核心动画的框架如下:
图一
蓝色代表继承某个类 红色代表遵守某个协议
图一和图二表达意思类似,图二中虚线框中的类为虚类,实线框为实例,绿色框未协议,核心动画中的虚类是不能使用的,使用其子类。
首先得有CALayer(因为CoreAnimation是作用在CALayer上的)
初始化一个CAAnimation对象,并设置一些动画相关属性
通过调用CALayer的addAnimation:forKey:方法,增加CAAnimation对象到CALayer中,这样就能开始执行动画了
通过调用CALayer的removeAnimationForKey:方法可以停止CALayer中的动画
CAAnimation中的一些属性:
* The begin time of the object, in relation to its parent object, if
* applicable. Defaults to 0. */
* 可以用来设置动画延迟执行时间,若想延迟2s,就设置为CACurrentMediaTime()+2,CACurrentMediaTime()为图层的当前时间
@property CFTimeInterval beginTime;
/* The basic duration of the object. Defaults to 0. */
@property CFTimeInterval duration;
/* The rate of the layer. Used to scale parent time to local time, e.g.
* if rate is 2, local time progresses twice as fast as parent time.
* Defaults to 1. */
@property float speed;
/* Additional offset in active local time. i.e. to convert from parent
* time tp to active local time t: t = (tp - begin) * speed + offset.
* One use of this is to "pause" a layer by setting `speed' to zero and
* `offset' to a suitable value. Defaults to 0. */
@property CFTimeInterval timeOffset;
/* The repeat count of the object. May be fractional. Defaults to 0. */
@property float repeatCount;
/* The repeat duration of the object. Defaults to 0. */
@property CFTimeInterval repeatDuration;
/* When true, the object plays backwards after playing forwards. Defaults
* to NO. */
@property BOOL autoreverses;
/* Defines how the timed object behaves outside its active duration.
* Local time may be clamped to either end of the active duration, or
* the element may be removed from the presentation. The legal values
* are `backwards', `forwards', `both' and `removed'. Defaults to
* `removed'. */
@property(copy) NSString *fillMode;
fillMode决定当前对象在非active时间段的行为。(要想fillMode有效,最好设置removedOnCompletion = NO)
。kCAFillModeRemoved 这个是默认值,也就是说当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到之前的状态
。kCAFillModeForwards 当动画结束后,layer会一直保持着动画最后的状态
。kCAFillModeBackwards 在动画开始前,只需要将动画加入了一个layer,layer便立即进入动画的初始状态并等待动画开始。
。kCAFillModeBoth 这个其实就是上面两个的合成.动画加入后开始之前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态
kCAFillModeBoth 这个其实就是上面两个的合成.动画加入后开始之前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态
CAAnimation代理方法
CAAnimation在分类中定义了代理方法。是给NSObject添加的分类,所以任何对象,成为CAAnimation的代理,都可以。
- (void)animationDidStart:(CAAnimation *)anim;
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;
CALayer上动画的暂停和恢复
-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
// 让CALayer的时间停止走动
layer.speed = 0.0;
// 让CALayer的时间停留在pausedTime这个时刻
layer.timeOffset = pausedTime;
}
-(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的子类
/** Subclass for basic (single-keyframe) animations. **/
@interface CABasicAnimation : CAPropertyAnimation
属性及其说明
/* The objects defining the property values being interpolated between.
* All are optional, and no more than two should be non-nil. The object
* type should match the type of the property being animated (using the
* standard rules described in CALayer.h). The supported modes of
* animation are:
*
* - both `fromValue' and `toValue' non-nil. Interpolates between
* `fromValue' and `toValue'.
*
* - `fromValue' and `byValue' non-nil. Interpolates between
* `fromValue' and `fromValue' plus `byValue'.
*
* - `byValue' and `toValue' non-nil. Interpolates between `toValue'
* minus `byValue' and `toValue'.
*
* - `fromValue' non-nil. Interpolates between `fromValue' and the
* current presentation value of the property.
*
* - `toValue' non-nil. Interpolates between the layer's current value
* of the property in the render tree and `toValue'.
*
* - `byValue' non-nil. Interpolates between the layer's current value
* of the property in the render tree and that plus `byValue'. */
@property(nullable, strong) id fromValue;
@property(nullable, strong) id toValue;
@property(nullable, strong) id byValue;
动画过程:
* 随着动画的进行,在长度为duration的持续时间内,keyPath相应属性的值从fromValue渐渐地变为toValue
* keyPath内容是CALayer的可动画Animatable属性
* 如果fillMode=kCAFillModeForwards同时removedOnComletion=NO,那么在动画执行完毕后,图层会保持显示动画执行后的状态。但在实质上,图层的属性值还是动画执行前的初始值,并没有真正被改变。
示例:
CABasicAnimation *anim = [CABasicAnimation animation];
anim.duration = 1.0;
// 移动
// anim.keyPath = @"position";
// anim.toValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
// 缩放
// 只能是layer的属性
anim.keyPath = @"transform.scale";
anim.toValue = @0.5;
anim.repeatCount = MAXFLOAT;
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
[_tripView.layer addAnimation:anim forKey:nil];
它也是CAPropertyAnimation的子类,与CABasicAnimation的区别是:
CABasicAnimation只能从一个数值(FromValue)变到另一个数值(toValue),而CAKeyFrameAnimation会使用一个NSArray保存这些数值。
CABasicAnimation可看做是只有2个关键帧的CAKeyframeAnimation
属性说明:
* values:上述的NSArray对象。里面的元素称为“关键帧”(keyframe)。动画对象会在指定的时间(duration)内,依次显示values数组中的每一个关键帧
* path:可以设置一个CGPathRef、CGMutablePathRef,让图层按照路径轨迹移动。path只对CALayer的anchorPoint和position起作用。如果设置了path,那么values将被忽略
* keyTimes:可以为对应的关键帧指定对应的时间点,其取值范围为0到1.0,keyTimes中的每一个时间值都对应values中的每一帧。如果没有设置keyTimes,各个关键帧的时间是平分
示例:
// 核心动画
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
// anim.keyPath = @"transform.rotation";
// anim.values = @[@(- M_PI_4 / 5), @(M_PI_4 / 5), @(- M_PI_4 / 5)];
anim.keyPath = @"position";
anim.path = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, 200, 500)].CGPath;
anim.duration = 2.0;
anim.repeatCount = MAXFLOAT;
[_tripView.layer addAnimation:anim forKey:nil];
动画组,是CAAnimation的子类,可以保存一组动画对象,将CAAnimationGroup对象加入层后,组中所有动画对象可以同时并发运行,默认情况下,一组动画对象是同时运行的,也可以通过设置动画对象的beginTime属性来更改动画的开始时间
属性说明:
* animations:用来保存一组动画对象的NSArray
示例:
// 动画组
CAAnimationGroup *group = [CAAnimationGroup animation];
CABasicAnimation *scale = [CABasicAnimation animation];
scale.keyPath = @"transform.scale";
scale.toValue = @0.5;
CABasicAnimation *rotation = [CABasicAnimation animation];
rotation.keyPath = @"transform.rotation";
rotation.toValue = @(M_PI_4);
group.animations = @[scale, rotation];
[_showI.layer addAnimation:group forKey:nil];
CATransition是CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果。iOS比Mac OS X的转场动画效果少一点
UINavigationController就是通过CATransition实现了将控制器的视图推入屏幕的动画效果
动画属性:
转场动画的类型(NSString *type)
fade : 交叉淡化过渡
push : 新视图把旧视图推出去
moveIn: 新视图移到旧视图上面
reveal: 将旧视图移开,显示下面的新视图
cube : 立方体翻滚效果
oglFlip : 上下左右翻转效果
suckEffect : 收缩效果,如一块布被抽走
rippleEffect: 水滴效果
pageCurl : 向上翻页效果
pageUnCurl : 向下翻页效果
cameraIrisHollowOpen : 相机镜头打开效果
cameraIrisHollowClose : 相机镜头关闭效果
示例:
static int i = 2;
NSString *imageName = [NSString stringWithFormat:@"%d", i];
_showI.image = [UIImage imageNamed:imageName];
if (i >= 3 || i < 2) {
i = 2;
} else {
i++;
}
//转场动画
CATransition *anim = [CATransition animation];
anim.duration = 4.0;
anim.subtype = @"fromRight";
anim.type = @"cube";
anim.startProgress = 0.5;
anim.endProgress = 0.8;
anim.repeatCount = MAXFLOAT;
[_showI.layer addAnimation:anim forKey:nil];