// CoreAnimationDemo
// CATransaction事务动画(显示动画(开头和结尾)和隐式动画)
// CAAnimation是一个基类定义一些动画的基本属性和方法
// CAPropertyAnimation属相动画是一个抽象的子类,支持动画的显示图层的关键路径(KeyPath)中定制的属性
// CABasicAnimation基础动画简单的为图层属性提供修改
// CAAnimationGroup组动画
// CAKeyframeAnimation关键帧动画
// CATransition过渡动画
//动画结束不移除动画效果(不返回)
CAAnimation *caa = nil;
caa.removedOnCompletion = NO;
caa.fillMode = kCAFillModeForwards;
CABasicAnimation *base;
//在你原来的基础上继续动画
base.cumulative =YES;
//事务动画
- (void)doTranscationAnimation {
//事务分为显示和隐式
//隐式事务指的是我们不去控制它的发生过程,只关心结果的时候,系统默认帮我们添加的一个动画的过程
//显示事务指的是我们明确的控制动画的发生过程
//显示动画
//开始
[CATransaction begin];
//动画时间
[CATransaction setAnimationDuration:0.5];
//速率函数
[CATransactionsetAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
//动画(图层)动画体
if(_layer.cornerRadius >= 30) {
_layer.cornerRadius = 10;
_layer.backgroundColor = [UIColor orangeColor].CGColor;
}else{
_layer.cornerRadius = 50;
_layer.backgroundColor= [UIColor blueColor].CGColor;
}
//提交
[CATransaction commit];
}
/*
keyPath::::::::::
边框颜色:borderColor
旋转:transform.rotation.ztransform.rotation.z
缩放:transform.scale
位置:position
背景颜色:backgroundColor
*/
//基础动画
- (void)doBaseAnimation {
// CABasicAnimation
//创建基础动画对象
CABasicAnimation *borederColorAnimation = [CABasicAnimation animation];
//设置关键路径(做什么动画)
borederColorAnimation.keyPath = @"borderColor";
//动画时间
borederColorAnimation.duration= 0.5f;
//动画的值
borederColorAnimation.byValue= (id)[UIColor blueColor].CGColor;
//是否自动重复
borederColorAnimation.autoreverses =YES;
//设置重复次数
borederColorAnimation.repeatCount= 5;
//把动画添加到动画对象上
// [_layer addAnimation:borederColorAnimation forKey:@"borederColor_Animation"];
//旋转
CABasicAnimation *rotationAnimotion = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimotion.duration= 1.5;
rotationAnimotion.toValue= @(M_PI);
rotationAnimotion.autoreverses=YES;
// [_layer addAnimation:rotationAnimotion forKey:@"rotation_Animotion"];
//缩放
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.duration= 1.5;
scaleAnimation.toValue= @(2.0);
scaleAnimation.autoreverses=YES;
// [_layer addAnimation:scaleAnimation forKey:@"scale_Animation”];
//组动画
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations= @[borederColorAnimation,rotationAnimotion,scaleAnimation];
group.duration=3;
[_layer addAnimation:groupforKey:@"Group_Animaton"];
}
//关键帧动画
- (void)doKeyFrameAnimation {
CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
positionAnimation.duration= 2;
//动画过程的设置
//获得原始数据获取图层位置
CGPointpoint =_layer.position;
//设置过程
positionAnimation.values= @[[NSValuevalueWithCGPoint:point],[NSValuevalueWithCGPoint:CGPointMake(0, 400)],[NSValuevalueWithCGPoint:CGPointMake(300, 400)],[NSValuevalueWithCGPoint:point]];
//规定每个帧动画的时间从什么时候开始
positionAnimation.keyTimes= @[@0,@0.3,@0.5,@1];
//将动画添加个目标对象
[_layer addAnimation:position AnimationforKey:@"position_Animation"];
CAKeyframeAnimation *backgroundColorAnimation = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
backgroundColorAnimation.duration= 2;
backgroundColorAnimation.values= @[(id)[UIColor redColor].CGColor,(id)[UIColor greenColor].CGColor,(id)[UIColor blueColor].CGColor];
[_layer addAnimation:backgroundColor AnimationforKey:@"backgroundColor_Animation"];
}
//过渡动画
- (void)doTransitionAnimation {
CATransition *transitionAnimation = [CATransition animation];
transitionAnimation.duration= 2;
//kCATransitionMoveIn
transitionAnimation.type=@"cube";
NSArray *subtypeArray = @[kCATransitionFromLeft,kCATransitionFromRight,kCATransitionFromBottom,kCATransitionFromTop,kCATransitionFromLeft];
transitionAnimation.subtype= subtypeArray[arc4random()%(subtypeArray.count)];
[_layer addAnimation:transition AnimationforKey:@"transition_Animation"];
/*
//transitionAnimation.type = @"oglFlip";
以下API效果可以安全使用
cube方块
suckEffect三角
rippleEffect水波抖动
pageCurl上翻页
pageUnCurl下翻页
oglFlip上下翻转
cameraIrisHollowOpen镜头快门开
cameraIrisHollowClose镜头快门开
以下API效果请慎用
spewEffect新版面在屏幕下方中间位置被释放出来覆盖旧版面.
genieEffect旧版面在屏幕左下方或右下方被吸走,显示出下面的新版面
unGenieEffect新版面在屏幕左下方或右下方被释放出来覆盖旧版面.
twist版面以水平方向像龙卷风式转出来.
tubey版面垂直附有弹性的转出来.
swirl旧版面360度旋转并淡出,显示出新版面.
charminUltra旧版面淡出并显示新版面.
zoomyIn新版面由小放大走到前面,旧版面放大由前面消失.
zoomyOut新版面屏幕外面缩放出现,旧版面缩小消失.
oglApplicationSuspend像按”home”按钮的效果.
*/
}
结构字段的键的取值
CAAnimation供支持使用关键路径访问选择的结构字段。这在为动画关键路径指定结构字段的时候非常有帮助,同时你可以使用setValue:forKeyPath:和valueForKeyPath来设置和读取相应的值。
CATransform3D公开如下的字段:
结构体字段 描述
transform.rotation.x The rotation, in radians, in the x axis.
transform.rotation.y The rotation, in radians, in the y axis.
transform.rotation.z The rotation, in radians, in the z axis.
transform.rotation The rotation, in radians, in the z axis. This is identical to setting the rotation.z field.
transform.scale.x Scale factor for the x axis.
transform.scale.y Scale factor for the y axis.
transform.scale.z Scale factor for the z axis.
transform.scale Average of all three scale factors.
transform.translation.x Translate in the x axis.
transform.translation.y Translate in the y axis.
translation.z Translate in the z axis.
translation Translate in the x and y axis. Value is an NSSize or CGSize.
CGPoint公开如下字段:
结构体字段描述
x The x component of the point.
y The y component of the point.
CGSize公开如下字段:
结构体字段描述
width The width component of the size.
height The height component of the size.
CGRect公开如下字段:
结构体字段描述
origin The origin of the rectangle as a CGPoint.
origin.x The x component of the rectangle origin.
origin.y The y component of the rectangle origin.
size The size of the rectangle as a CGSize.
size.width The width component of the rectangle size.
size.height The height component of the rectangle size
你不可以通过Objective-C 2.0的属性方法来指定一个结构字段的关键路径。如下的代码是无法正常执行的:
myLayer.transform.rotation.x = 0;
相反你必须使用setValue:forKeyPath:或者valuForKeyPath:,如下:
[myLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"transform.rotation.x"];