博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
小知识六、CALayer动画
阅读量:6968 次
发布时间:2019-06-27

本文共 5623 字,大约阅读时间需要 18 分钟。

CAShapeLayer 圆形指示器

let ovalShapeLayer: CAShapeLayer = CAShapeLayer()   let anotherOvalShapeLayer: CAShapeLayer = CAShapeLayer()   override func viewDidLoad() {       super.viewDidLoad()       loadingIndicator()       beginSimpleAnimation()              complexLoadingIndicator()       complexAnimations()   }      // MARK: - 简单的加载指示器   func loadingIndicator() {       ovalShapeLayer.strokeColor = UIColor.white.cgColor       ovalShapeLayer.fillColor = UIColor.clear.cgColor       ovalShapeLayer.lineWidth = 7       let ovalRadius = loadingView.bounds.size.height / 2.0 * 0.8       ovalShapeLayer.path = UIBezierPath(ovalIn: CGRect(x: loadingView.frame.size.width/2 - ovalRadius, y: loadingView.frame.size.height/2 - ovalRadius, width: ovalRadius * 2, height: ovalRadius * 2)).cgPath       ovalShapeLayer.strokeEnd = 0.4       ovalShapeLayer.lineCap = kCALineCapRound       loadingView.layer.addSublayer(ovalShapeLayer)   }      func beginSimpleAnimation() {       let rotate = CABasicAnimation(keyPath: "transform.rotation")       rotate.duration = 1.5       rotate.fromValue = 0       rotate.toValue = 2 * M_PI       rotate.repeatCount = HUGE       rotate.speed = 1       loadingView.layer.add(rotate, forKey: nil)       //loadingView.layer.anchorPoint = CGPoint(x: 0, y: 0)   }      // MARK: - 复杂的加载提示   func complexLoadingIndicator() {       anotherOvalShapeLayer.strokeColor = UIColor.white.cgColor       anotherOvalShapeLayer.fillColor = UIColor.clear.cgColor       anotherOvalShapeLayer.lineWidth = 3              let anotherOvalRadius = complexLoadingView.frame.size.height/2 * 0.8       anotherOvalShapeLayer.path = UIBezierPath(ovalIn: CGRect(x: complexLoadingView.frame.size.width/2 - anotherOvalRadius, y: complexLoadingView.frame.size.height/2 - anotherOvalRadius, width: anotherOvalRadius * 2, height: anotherOvalRadius * 2)).cgPath       anotherOvalShapeLayer.lineCap = kCALineCapRound              complexLoadingView.layer.addSublayer(anotherOvalShapeLayer)   }      func complexAnimations() {       let strokeStartAnimate = CABasicAnimation(keyPath: "strokeStart")       strokeStartAnimate.fromValue = -0.5       strokeStartAnimate.toValue = 1              let strokeEndAnimate = CABasicAnimation(keyPath: "strokeEnd")       strokeEndAnimate.fromValue = 0.0       strokeEndAnimate.toValue = 1              let strokeAnimateGroup = CAAnimationGroup()       strokeAnimateGroup.duration = 1.5       strokeAnimateGroup.repeatCount = HUGE       strokeAnimateGroup.animations = [strokeStartAnimate, strokeEndAnimate]       anotherOvalShapeLayer.add(strokeAnimateGroup, forKey: nil)   }复制代码

CAReplicatorLayer

/*CAReplicatorLayer是一个新面孔,它也是CALayer的子类,正如它的名称一样,CAReplicatorLayer可以对它自己的子Layer进行复制操作。 */

// MARK: - 正在播放音乐或广播动画    func firstReplicatorAnimation() {                let replicatorLayer = CAReplicatorLayer()        replicatorLayer.bounds = CGRect(x: replicatorAnimationView.frame.origin.x, y: replicatorAnimationView.frame.origin.y, width: replicatorAnimationView.frame.size.width, height: replicatorAnimationView.frame.size.height)        replicatorLayer.anchorPoint = CGPoint(x: 0, y: 0)        replicatorLayer.backgroundColor = UIColor.clear.cgColor        replicatorLayer.instanceCount = 3 // 设置三份        replicatorLayer.instanceTransform = CATransform3DMakeTranslation(40, 0, 0) // 设置间隔        replicatorLayer.instanceDelay = 0.3        replicatorLayer.masksToBounds = true        replicatorAnimationView.layer.addSublayer(replicatorLayer)                        let rectangle = CALayer()        rectangle.bounds = CGRect(x: 0, y: 0, width: 30, height: 90)        rectangle.anchorPoint = CGPoint(x: 0, y: 0)        rectangle.position = CGPoint(x: replicatorAnimationView.frame.origin.x + 10, y: replicatorAnimationView.frame.origin.y + 110)        rectangle.cornerRadius = 2        rectangle.backgroundColor = UIColor.white.cgColor        replicatorLayer.addSublayer(rectangle)                let moveRectangle = CABasicAnimation(keyPath: "position.y")        moveRectangle.toValue = rectangle.position.y - 70        moveRectangle.duration = 0.7        moveRectangle.autoreverses = true        moveRectangle.repeatCount = HUGE        rectangle.add(moveRectangle, forKey: nil)    }        func activityIndicatorAnimation() {                let replicatorLayer = CAReplicatorLayer()        replicatorLayer.bounds = CGRect(x: 0, y: 0, width: activityIndicatorView.frame.size.width, height: activityIndicatorView.frame.size.height)        replicatorLayer.position = CGPoint(x: activityIndicatorView.frame.size.width/2, y: activityIndicatorView.frame.size.height/2)        replicatorLayer.backgroundColor = UIColor.clear.cgColor                        replicatorLayer.instanceCount = 15        let angle = CGFloat(2 * M_PI) / CGFloat(15)        replicatorLayer.instanceTransform = CATransform3DMakeRotation(angle, 0, 0, 1)        replicatorLayer.instanceDelay = 1/15        activityIndicatorView.layer.addSublayer(replicatorLayer)                        let circle = CALayer()        circle.bounds = CGRect(x: 0, y: 0, width: 15, height: 15)        circle.position = CGPoint(x: activityIndicatorView.frame.size.width/2, y: activityIndicatorView.frame.size.height/2 - 55)        circle.cornerRadius = 7.5        circle.backgroundColor = UIColor.white.cgColor        circle.transform = CATransform3DMakeScale(0.01, 0.01, 0.01)        replicatorLayer.addSublayer(circle)                        let scale = CABasicAnimation(keyPath: "transform.scale")        scale.fromValue = 1        scale.toValue = 0.1        scale.duration = 1        scale.repeatCount = HUGE        circle.add(scale, forKey: nil)    }复制代码

转载地址:http://afssl.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
nginx详细配置教程
查看>>
Zabbix3.0 监控HW交换机
查看>>
js继承方式详解
查看>>
sqoop的使用
查看>>
php生成图像验证码
查看>>
linux12-xinetd,系统备份与恢复
查看>>
unix基础教程9 标准I/O
查看>>
寒冰linux视频教程笔记4 目录
查看>>
zabbix中通过shell脚本进行微信监控告警
查看>>
Windows下Nginx的启动、停止等命令
查看>>
我的友情链接
查看>>
sed运用实例一——基于变量的动态替换
查看>>
centos下搭建svn的安装过程
查看>>
Html 常见问题
查看>>
网站安全检测之图片验证码
查看>>
PB 级数据处理挑战,Kubernetes如何助力基因分析?
查看>>
vue cle新建vue项目
查看>>
配置网络、远程登录
查看>>
阿里架构师干货分享——eureka分布式框架demo
查看>>