IOS Animation-CAKeyframeAnimation例子(简单动画实现)

本文提供两个iOS动画实现案例:一是使用CAKeyframeAnimation让图层左右摇摆;二是让图层沿圆形路径移动。通过具体代码展示了如何配置动画参数,如路径、重复次数等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在阅读本文之前,可以看看 CABasicAnimation的例子

也可以看看IOS Animation-CABasicAnimation、CAKeyframeAnimation详解&区别&联系

 

1)让一个layer左右晃动

 1     //让一个layer左右晃动
 2     func addLayerKeyframeAnimationRock(layer:CALayer) {
 3         let animation = CAKeyframeAnimation(keyPath: "position.x")
 4         animation.values = [0, 10, -10, 0]
 5         //additive设置为true是使Core Animation 在更新 presentation layer 之前将动画的值添加到 model layer 中去。可以看到上面的values是0,10,-10,0. 没有设置的话values=layer.position.x+0, layer.position.x+10, layer.position.x-10
 6         animation.additive = true
 7         animation.duration = 0.3
 8         animation.repeatCount = 999999
 9         
10         layer.addAnimation(animation, forKey: "addLayerKeyframeAnimationRock")
11     }

 

2)让一个layer圆周(圆圈)运动

 1     //让一个layer圆周(圆圈)运动
 2     func addLayerKeyframeAnimationOrbit(layer:CALayer) {
 3         let animation = CAKeyframeAnimation(keyPath: "position")
 4         let boundingRect = CGRectMake(layer.frame.origin.x, layer.frame.origin.y, 100, 100)
 5         animation.path = CGPathCreateWithEllipseInRect(boundingRect,nil)
 6         animation.duration = 3
 7         animation.repeatCount = HUGE
 8         //其值为kCAAnimationPaced,保证动画向被驱动的对象施加一个恒定速度,不管路径的各个线段有多长,并且无视我们已经设置的keyTimes
 9         animation.calculationMode = kCAAnimationPaced
10         //kCAAnimationRotateAuto,确定其沿着路径旋转(具体要自己来体验,这里难解释)
11         animation.rotationMode = kCAAnimationRotateAuto
12         
13         layer.addAnimation(animation, forKey: "addLayerKeyframeAnimationOrbit")
14     }

 

可以关注本人的公众号,多年经验的原创文章共享给大家。

posted on 2016-04-10 23:32 alun-chen 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/alunchen/p/5376529.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值