不经常写东西想到什么写什么,诸多不足之处多见谅;
还可以看看,别人翻译的iOS Core Animation: Advanced Techniques
Core Animation 核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往事半功倍,也就是说,使用少量的代码就可以实现非常强大的功能。
- core Animation是跨平台的,可以用在Mac OS X和ios平台
- Core Animation的动画执行过程都是在后台操作的,不会阻塞主线程
- 要注意的是,Core Animation的动画是直接作用在CALayer上的。并非UIVIew
核心动画继承结构图
下图为继承关系图,虚线为继承某个类,实现为遵守某个协议
xcode5及之前的版本,使用core Animation,需要先添加QuartzCor框架和引入对应框架的头文件<QuartzCore/QuartzCore.h>
开发步骤:
1.首先得有CALayer
2.初始化一个CAAnimation对象,并设置一些动画相关属性
3.通过调用CALyaer的addAnimation:forKey:方法,增加CAAnimation对象到CALayer中,这样就能开始动画了
CABasicAnimation 简单示例
// 创建Animation对象
CABasicAnimation *basicA = [CABasicAnimation animation];
//设置属性值
basicA.keyPath=@"position.x";
basicA.toValue = @(300);
//设置动画完成时,是否删除动画(不设置默认是删除,会回到初始位置)
basicA.removedOnCompletion=NO;
basicA.fillMode = kCAFillModeForwards;
使用transform.scale实现一个心跳效果如下
CABasicAnimation *animation=[CABasicAnimation animation];
animation.keyPath = @"transform.scale";
animation.toValue=@0.2;
//设置动画执行次数
animation.repeatCount = MAXFLOAT;
//设置一次动画执行时间
animation.duration = 0.5;
//自动反转(怎么去怎么回)
animation.autoreverses=YES;
[self.imageView.layer addAnimation:animation forKey:nil];
CAKeyframeAnimation
CAKeyframeAnimation 设置多个值。下例为ios桌面icon抖动效果动画
CAKeyframeAnimation *animation=[CAKeyframeAnimation animation];
animation.keyPath=@"transform.rotation";//@"transform.rotation";
CGFloat t = angle2Rad(-5);
CGFloat t1 = angle2Rad(5);
animation.values=@[@(t),@(t1),@(t)];
animation.duration=0.5;
animation.repeatCount = MAXFLOAT;
//自动反转(怎么去怎么回)
// animation.autoreverses=YES;
[self.imageView.layer addAnimation:animation forKey:nil];
CAKeyframeAnimation 还支持设置路径动画
CAKeyframeAnimation *animation=[CAKeyframeAnimation animation];
animation.keyPath=@"position";//@"transform.rotation";
UIBezierPath *path=[UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(50, 50)];
[path addLineToPoint:CGPointMake(300, 500)];
animation.path = path.CGPath;
animation.removedOnCompletion=NO;
animation.fillMode = kCAFillModeForwards;
[self.imageView.layer addAnimation:animation forKey:nil];
CATransition 转场动画
[self.imageViewTwo.layer removeAllAnimations];
NSString *imageName= [NSString stringWithFormat:@"%d",name];
UIImage *image=[UIImage imageNamed:imageName];
self.imageViewTwo.image=image;
CATransition *animation = [CATransition animation];
animation.type=@"pageCurl";
animation.duration=1;
//设置动画起始位置
animation.startProgress = 0.5;
//设置动画结束位置
animation.endProgress=0.9;
[self.imageViewTwo.layer addAnimation:animation forKey:nil];
type值
cube 立体翻转、suckEffect 像被吸入瓶子、rippleEffect 波纹效果、pageCurl 翻页效果、pageUnCurl反翻页、cameraIrisHollowOpen 开镜头、cameraIrisHollowClose 关镜头、oglFlip翻转
CAAnimationGroup
需要同时实现多个动画,需要用CAAnimationGroup
核心动画与UIVIew动画的区别
1.核心动画只作用在layer上。
2.核心动画看到的都是假象,它并没有去修改改版uiview的实际位置
什么时候使用核心动画?
1.当不需要与用户进行交互,使用核心动画
2.需要根据路径做动画时,使用核心动画
3.当做转场动画时,使用核心动画(转场类型比较多)
*[UIScreen mainScreen].scale 像素和点的比例如iphone 6上一个点对应2个像素,在更好的机器如iphone x等一个点对应3个像素 *
通过atan2
函数,获取用view的transform的b,a值计算出旋转角度弧度
CGAffineTransform transform = self.selectedBut.transform;
CGFloat angle = atan2(transform.b,transform.a);
下面是写的几个简单例子CoreAnimationDome