概述
1.了解layer
Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果
Layer与view:
view:在iOS中,你能看得见摸得着的东西基本上都是UIView,view可以感受触摸等进行交互
layer:当UIView需要显示到屏幕上时,会调用drawRect:方法进行绘图,并且会将所有内容绘制在自己的图层上,绘图完毕后,系统会将图层拷贝到屏幕上,于是就完成了UIView的显示,view本身不具备显示功能
2.核心动画结构
连线代表继承关系,所有的CA动画都遵守CAMediaTiming协议
3.CAAnimation和CAPropertyAnimation 是抽象类,没有动画效果,用其子类才有动画效果
CAAnimationGroup是个动画组,可以同时进行缩放,旋转
CATransition是转场动画,界面之间跳转(切换)都可以用转场动画。
CABasicAnimation基本动画,做一些简单效果,比如:位移、透明度、缩放、旋转、背景色等等。
CAKeyframeAnimation帧动画,做一些连续的流畅的动画.
4.CALayer重要属性,动画要基于这些重要属性
@property CGPoint position
/*The position in the superlayer that the anchor point of the layer's*/-position在锚点指定点
@property CGPoint anchorPoint //锚点
@property CATransform3D transform;//形变属性
CAPropertyAnimation
CAPropertyAnimation动画包括CABasicAnimation和CAKeyframeAnimation
CABasicAnimation
属性
@property(nullable, strong) id fromValue;
@property(nullable, strong) id toValue;
@property(nullable, strong) id byValue;
部分属性代码示例及效果图
1.位移
- (IBAction)move:(id)sender {
CALayer *layer = _bgView.layer;
layer.anchorPoint = CGPointMake(0, 0);//在anchor指定的点在point上,默认是中点在point指定的点上
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(250, 250)];
basicAnimation.duration = 1.0f;//默认是0.25秒
basicAnimation.fillMode = kCAFillModeForwards;
basicAnimation.removedOnCompletion = NO;//不恢复原来的位置
[layer addAnimation:basicAnimation forKey:@"positionAnimation"];
}
2.变色
- (IBAction)changeColor:(id)sender {
CALayer *layer = _bgView.layer;
layer.anchorPoint = CGPointMake(0, 0);//在anchor指定的点在point上,默认是中点在point指定的点上
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
basicAnimation.fromValue = (id)[UIColor redColor].CGColor;
basicAnimation.toValue = (id)[UIColor yellowColor].CGColor;
basicAnimation.duration = 1.0f;
basicAnimation.fillMode = kCAFillModeForwards;
basicAnimation.removedOnCompletion = NO;//不恢复原来的位置
[layer addAnimation:basicAnimation forKey:@"backgroundAnimation"];
}
3.缩放
- (IBAction)changeScale:(id)sender {
CALayer *layer = _bgView.layer;
layer.anchorPoint = CGPointMake(0.5, 0.5);//在anchor指定的点在point上,默认是中点在point指定的点上
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.fromValue = [NSNumber numberWithDouble:1.0];
animation.toValue = [NSNumber numberWithDouble:2.0];
[layer addAnimation:animation forKey:@"scaleAnimation"];
}
4.旋转
- (IBAction)rotation:(id)sender {
CALayer *layer = _bgView.layer;
layer.anchorPoint = CGPointMake(0.5, 0.5);
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//绕z轴旋转
animation.toValue = [NSNumber numberWithDouble:2*M_PI];
animation.duration = 1.0;
[layer addAnimation:animation forKey:@"rotationAnimation"];
}
/注/:关于animationWithKeyPath的说明,是layer层的属性,摘自官方文档的说明:
The CABasicAnimation class provides basic, single-keyframe animation capabilities for a layer property. You create an instance of CABasicAnimation using the inherited animationWithKeyPath: method, specifying the key path of the layer property to be animated. Animatable Properties in Core Animation Programming Guide summarize the animatable properties for CALayer and its filter properties.