本文主要是介绍Swift :UIView扩展+直播间麦克风波纹扩散动画,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
功能需要,用贝塞尔曲线写了一个波纹动画分享一下
效果图:
知识点:
- 扩展不能直接加属性,用runtime方式添加
//bord宽度 默认1.5var FQBordWith:CGFloat?{get{return (objc_getAssociatedObject(self,"FQBordWith") as? CGFloat)}set(newValue){objc_setAssociatedObject(self, "FQBordWith", newValue, .OBJC_ASSOCIATION_ASSIGN)}}
- 注意 block 的循环引用 [weak self]
self.FQTimer = Timer.scheduledTimer(withTimeInterval: TimeInterval(longTime), repeats: true, block: { [weak self](timer:Timer) inif let strongSelf = self { //精髓用法index += 1;if index > number {strongSelf.invalidTimer()return}strongSelf.start()}})
文件代码如下:
//
// UIView+FQWave.swift
// FQWaveDemo
//
// Created by jiashiqi on 2020/4/7.
// Copyright © 2020 jiashiqi. All rights reserved.
//import Foundation
import UIKitextension UIView{//bord宽度 默认1.5var FQBordWith:CGFloat?{get{return (objc_getAssociatedObject(self,"FQBordWith") as? CGFloat)}set(newValue){objc_setAssociatedObject(self, "FQBordWith", newValue, .OBJC_ASSOCIATION_ASSIGN)}}//动画时间 默认2var FQAnimationTime:CGFloat? {get{return (objc_getAssociatedObject(self,"FQAnimationTime") as? CGFloat)}set(newValue){objc_setAssociatedObject(self, "FQAnimationTime", newValue, .OBJC_ASSOCIATION_ASSIGN)}}//开始透明度 默认 0.8var FQStartAlph:CGFloat? {get{return (objc_getAssociatedObject(self,"FQStartAlph") as? CGFloat)}set(newValue){objc_setAssociatedObject(self, "FQStartAlph", newValue, .OBJC_ASSOCIATION_ASSIGN)}}//比例 默认1.5var FQScale:CGFloat? {get{return (objc_getAssociatedObject(self,"FQScale") as? CGFloat)}set(newValue){objc_setAssociatedObject(self, "FQScale", newValue, .OBJC_ASSOCIATION_ASSIGN)}}//颜色 默认redvar FQColor:UIColor? {get{return (objc_getAssociatedObject(self,"FQColor") as? UIColor)}set(newValue){objc_setAssociatedObject(self, "FQColor", newValue, .OBJC_ASSOCIATION_ASSIGN)}}var FQTimer:Timer?{get{return (objc_getAssociatedObject(self,"FQTimer") as? Timer)}set(newValue){objc_setAssociatedObject(self, "FQTimer", newValue, .OBJC_ASSOCIATION_ASSIGN)}}//开始动画public func startAnimation(circleNumber:NSInteger){if (self.FQTimer != nil) {self.invalidTimer()}var index = 0let number = circleNumber == 0 ? 1 : circleNumber// 每一个圈需要的时间let longTime:CGFloat = (self.FQAnimationTime ?? 2.0) / CGFloat(number)self.FQTimer = Timer.scheduledTimer(withTimeInterval: TimeInterval(longTime), repeats: true, block: { [weak self](timer:Timer) inif let strongSelf = self {index += 1;if index > number {strongSelf.invalidTimer()return}strongSelf.start()}})}//结束动画public func stopAnimation(){self.layer.removeAllAnimations()self.layer.superlayer?.perform(#selector(CALayer.removeFromSuperlayer))self.invalidTimer()}private func invalidTimer(){self.FQTimer?.invalidate();self.FQTimer = nil;}private func start(){//中心点let layer:CAShapeLayer = CAShapeLayer();layer.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2)layer.bounds = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)layer.opacity = 0layer.backgroundColor = UIColor.clear.cgColor//画圆圈let path:UIBezierPath = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))layer.path = path.cgPathlayer.strokeColor = (self.FQColor ?? UIColor.red).withAlphaComponent((self.FQStartAlph ?? 0.8)).cgColorlayer.lineWidth = (self.FQBordWith ?? 1.5)layer.fillColor = UIColor.clear.cgColorself.layer.addSublayer(layer)//动画//圈透明度let opacity:CABasicAnimation = CABasicAnimation(keyPath: "opacity")opacity.fromValue = (self.FQStartAlph ?? 0.8)opacity.toValue = 0//圈的发散let scale:CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")scale.fromValue = 1.1scale.toValue = (self.FQScale ?? 1.5)//圈的宽度let border:CABasicAnimation = CABasicAnimation(keyPath: "lineWidth")border.fromValue = (self.FQBordWith ?? 1.5)border.toValue = 1border.duration = 0.2//动画组let animationGroup:CAAnimationGroup = CAAnimationGroup()animationGroup.animations = [opacity,scale,border]animationGroup.duration = CFTimeInterval(self.FQAnimationTime ?? 2.0)animationGroup.isRemovedOnCompletion = true;animationGroup.fillMode = CAMediaTimingFillMode.forwards;animationGroup.repeatCount = MAXFLOAT;layer.add(animationGroup, forKey: nil)}
}
dome下载:下载地址
如有问题,可留言解决。
这篇关于Swift :UIView扩展+直播间麦克风波纹扩散动画的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!