iOS Developer - AnimationiOS动画程序员

核心动画(CoreAnimation)

2016-08-09  本文已影响150人  三生石畔

Core Animation简介

Core Animation的使用步骤

CAAnimation——简介

CAAnimation——动画填充模式

CAAnimation——速度控制函数

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;

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;
}

CALayer上动画的恢复

#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——关键帧动画

- CABasicAnimation可看做是只有2个关键帧的CAKeyframeAnimation

转场动画——CATransition

转场动画过渡效果

Snip20160809_1.png

使用UIView动画函数实现转场动画——单视 图

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

使用UIView动画函数实现转场动画——双视图

 + (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration: (NSTimeInterval)duration options: (UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion;

CADisplayLink

下面是写的一个关于CoreAnimation的一个demo

Untitled.gif
上一篇下一篇

猜你喜欢

热点阅读