iOS开发收集iOS进阶指南iOS Developer

核心动画Core Animation

2016-08-05  本文已影响666人  張贺
图片来自500px

文 || 張贺

Core Animation简介

核心动画继承结构

核心动画继承结构图

图注:

Core Animation的使用步骤

CAAnimation

CAAnimation - 简介
CAAnimation - 动画的填充模式
CAAnimation - 速度控制函数
CAAnimation - 动画代理方法
/* Delegate methods for CAAnimation. */

@interface NSObject (CAAnimationDelegate)

/* Called when the animation begins its active duration. */

- (void)animationDidStart:(CAAnimation *)anim;

/* Called when the animation either completes its active duration or
 * is removed from the object it is attached to (i.e. the layer). 'flag'
 * is true if the animation reached the end of its active duration
 * without being removed. */

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;

@end
CAAnimation - CALayer上动画的暂停和恢复
#pragma mark 暂停CALayer的动画
-(void)pauseLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];

    // 让CALayer的时间停止走动
    layer.speed = 0.0;
    // 让CALayer的时间停留在pausedTime这个时刻
    layer.timeOffset = pausedTime;
}
#pragma mark 恢复CALayer的动画
-(void)resumeLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = layer.timeOffset;
    // 1. 让CALayer的时间继续行走
    layer.speed = 1.0;
    // 2. 取消上次记录的停留时刻
    layer.timeOffset = 0.0;
    // 3. 取消上次设置的时间
    layer.beginTime = 0.0;
    // 4. 计算暂停的时间(这里也可以用CACurrentMediaTime()-pausedTime)
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    // 5. 设置相对于父坐标系的开始时间(往后退timeSincePause)
    layer.beginTime = timeSincePause;
}

CAPropertyAnimation

CABasicAnimation基础动画

CAKeyframeAnimation关键帧动画

CAAnimationGroup动画组

CATransition转场动画

使用UIView动画函数实现转场动画

单视图

+ (void)transitionWithView:(UIView *)view 
duration:(NSTimeInterval)duration        
options:(UIViewAnimationOptions)options 
animations:(void (^ __nullable)(void))animations 
completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

双视图

+ (void)transitionFromView:(UIView *)fromView 
toView:(UIView *)toView 
duration:(NSTimeInterval)duration 
options:(UIViewAnimationOptions)options 
completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
 // toView added to fromView.superview, fromView removed from its superview

UIView动画与核心动画对比?

1.UIView和核心动画区别?
核心动画只能添加到CALayer
核心动画一切都是假象,并不会改变真实的值

2.什么时候使用UIView的动画?
如果需要与用户交互就使用UIView的动画
不需要与用户交互可以使用核心动画

3.什么场景使用核心动画最多?
在转场动画中,核心动画的类型比较多
根据一个路径做动画,只能用核心动画(帧动画)
动画组:同时做多个动画

上一篇 下一篇

猜你喜欢

热点阅读