iOS 动画五:Keyframe Animations

作者 : 开心源码 本文共1282个字,预计阅读时间需要4分钟 发布时间: 2022-05-11 共83人阅读

keyFrame

为了实现这个效果,能这样写:

UIView.animate(withDuration: 0.5, animations: {      view.center.x += 200.0 }, completion: { _ in     UIView.animate(withDuration: 0.5,animations: {        view.center.y += 100.0}, completion: { _ in       UIView.animate(withDuration: 0.5, animations: {             view.center.x -= 200.0       }, completion: { _ in            UIView.animate(withDuration: 0.5, animations: {           view.center.y -= 100.0        })      })})})

这样写太麻烦了不是,有没更好的方法,当然有了,就是 Keyframe Animations 了。

我们能把飞机起飞的过程分为几个过程,前一阶段,飞机在跑道上加速;第二阶段,飞机向上倾斜爬升;第三阶段,飞机继续爬升,这次速率更快;第四阶段,动画的后 10% 部分,飞机飞出我们的视线,就像它飞进了云层中。

整体动画像上图那样。

现在用 Keyframe Animations 实现我们需要的效果,上代码:

func planeDepart() {    let originalCenter = planeImage.center   UIView.animateKeyframes(withDuration: 1.5, delay: 0.0, animations: {      //  add keyframes      UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.25, animations: {             self.planeImage.center.x += 80.0           self.planeImage.center.y -= 10.0 } )     }, completion: nil )     UIView.addKeyframe(withRelativeStartTime: 0.1, relativeDuration: 0.4) {        self.planeImage.transform = CGAffineTransform(rotationAngle: -.pi / 8)      }     UIView.addKeyframe(withRelativeStartTime: 0.25, relativeDuration: 0.25) {        self.planeImage.center.x += 100.0                self.planeImage.center.y -= 50.0            self.planeImage.alpha = 0.0      }}

UIView.addKeyframe 发布方法中的参数意义,withRelativeStartTime: 0.1, relativeDuration: 0.4,第一个参数 0.1 代表从总动画时间的 10% 开始执行,第二个参数代表动画执行总时间。

效果示用意如下:

relative times

附:keyframe animations demo 下载

2018.06.25
上海 虹桥

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » iOS 动画五:Keyframe Animations

发表回复