本文及其之后的讲解是依据Raywenderich 的 Animation Tutorial这一本书和苹果官方文档总结而来,特此声明!
UIView的关键帧动画
- animateKeyframesWithDuration:delay:options:animations:completion:
class func animateKeyframesWithDuration(_ duration: NSTimeInterval,
delay delay: NSTimeInterval,
options options: UIViewKeyframeAnimationOptions,
animations animations: () -> Void,
completion completion: ((Bool) -> Void)?)
官方文档:
This method creates an animation block that you can use to set up a keyframe-based animation. The keyframes themselves are not part of the initial animation block you create using this method. Inside the animations block, you must add the keyframe time and animation data by calling the **addKeyframeWithRelativeStartTime: relativeDuration: animations: **method one or more times. Adding keyframes causes the animation to animate the view from its current value to the value of the first keyframe, then to the value of the next keyframe, and so on at the times you specify.
If you do not add any keyframes in the animations block, the animation proceeds from start to end like a standard animation block. In other words, the system animates from the current view values to any new values over the specified duration.
为当前视图建立可以容纳一个或多个关键帧动画对象的动画块,然后根据指定的时间一帧帧的执行指定动画,需要与addKeyframeWithRelativeStartTime: relativeDuration: animations: 结合使用,注意:如果在block中没用添加关键帧动画对象,动画还是会执行,只不过跟调用animateWithDuration(duration: delay: options: animations: completion: 效果一样!简单点来说,调用该API就是创建了一个动画容器,然后可以向这个容器中添加多个动画!
示意图:
1 .CalculationModeLinear:在帧动画之间采用线性过渡
2 .CalculationModeDiscrete:在帧动画之间不过渡,直接执行各自动画
3 .CalculationModePaced:将不同帧动画的效果尽量融合为一个比较流畅的动画
4 .CalculationModeCubic:不同帧动画之间采用Catmull-Rom算法过渡
5 .CalculationModeCubicPaced:3和4结合,试了就知道什么效果了
示意图
- addKeyframeWithRelativeStartTime:relativeDuration:animations:
class func addKeyframeWithRelativeStartTime(_ frameStartTime: Double,
relativeDuration frameDuration: Double,
animations animations: () -> Void)
官方文档:
To animate view properties during a keyframe animation, call this method from within the animation block you pass to the animateKeyframesWithDuration:delay:options:animations:completion: method. To animate between different values, or to tweak the timing of your view property animations, you can call this method multiple times within a block.
The view properties you change in the animations block animate over the timespan you specify in frameDuration parameter. The properties do not begin animating until the time you specify in the frameStartTime parameter. After the frame start time, the animation executes over its specified duration or until interrupted by another animation.
与animateKeyframesWithDuration:delay:options:animations:completion:结合使用,用来指定帧动画开始时间,持续时间和执行操作,调用一次就可以添加一个帧动画!
参数解读:
1 frameStartTime:帧动画开始时间,取值范围为(0,1),开始时间是相对于整个动画时间,整个关键帧动画时长6秒,设置开始时间为0.5,那么这一帧动画的实际开始时间为第3秒!
2 frameDuration:帧动画持续时间,取值范围为(0,1),持续时间也是相对于整个动画时间,算法同上!
由于帧动画开始时间和持续时间均为相对时间,取值范围均为(0,1),可以用一个简单的公式来计算实际时间:
实际时间 = 整个动画时长 * 参数值(0-1)
Demo
demo使用了关键帧动画API以及普通动画API
-
效果图
- Demo下载
关键帧动画Demo