【Objective-c】动画学习笔记(三)_CABasicAnimation
admin
2023-08-30 01:21:57
0

前言:iOS与MacOS的坐标系不同,iOS使用的是左手坐标系,坐标原点是在左上角,MacOS 使用的是右手坐标系,原点是在左下角。
简单的动画实现:矩形平移动画

- (void)viewDidLoad { [super viewDidLoad]; //1.创建一个layer CALayer *mylayer = [CALayer layer]; //2.设置layer属性 mylayer.bounds = CGRectMake(0, 0, 50, 80); mylayer.backgroundColor = [UIColor yellowColor].CGColor; mylayer.position = CGPointMake(50, 50); mylayer.anchorPoint = CGPointMake(0, 0); mylayer.cornerRadius = 20; //3.添加layer [self.view.layer addSublayer:mylayer]; self.mylayer = mylayer; }- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //1.创建核心动画(剧本) CABasicAnimation *anima = [CABasicAnimation animation]; //设置剧本具体动画内容 //设置layer的position属性,即平移 anima.keyPath = @"position"; anima.fromValue = https://www.it610.com/article/[NSValue valueWithCGPoint:CGPointMake(0, 0)]; anima.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)]; //设置动画执行完毕之后不删除动画 anima.removedOnCompletion = NO; //设置保存动画的最新状态 anima.fillMode = kCAFillModeForwards; //2.添加核心动画到layer(执行剧本) [self.mylayer addAnimation:anima forKey:nil]; }

【【Objective-c】动画学习笔记(三)_CABasicAnimation】这里有一个重要的知识点:position和anchorPoint,不懂的可以浏览这篇博客大头青年,瞬间秒懂。这里就简单讲讲我的理解:一个layer的位置frame是由position和anchorPoint共同决定的(不管layer之前的frame是多少都没有影响)只要有一个改变了,那么layer的位置就会改变。第二了就是position和anchorPoint是不同坐标系的重合点,即指同一个点。
监听动画的执行过程,设置代理
在上方代码的基础上添加代理
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //1.创建核心动画(剧本) CABasicAnimation *anima = [CABasicAnimation animation]; //设置剧本具体动画内容 //设置layer的position属性,即平移 anima.keyPath = @"position"; anima.fromValue = https://www.it610.com/article/[NSValue valueWithCGPoint:CGPointMake(0, 0)]; anima.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)]; //设置动画执行完毕之后不删除动画 anima.removedOnCompletion = NO; //设置保存动画的最新状态 anima.fillMode = kCAFillModeForwards; //设置代理 anima.delegate = self; NSString *str = NSStringFromCGPoint(self.myLayer.position); NSLog(@"执行前:%@",str); //2.添加核心动画到layer(执行剧本) [self.mylayer addAnimation:anima forKey:nil]; }- (void)animationDidStart:(CAAnimation*)anim{ NSLog(@"开始执行动画"); }- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{ NSString *str = NSStringFromCGPoint(self.myLayer.position); NSLog(@"执行后:%@",str); } /* 输出结果: **2016-12-01 23:50:47.172 CAPropertyAnimationDemo[2829:99758] ****动画执行前:****{50, 50}** **2016-12-01 23:50:47.173 CAPropertyAnimationDemo[2829:99758] ****动画开始执行了** **2016-12-01 23:50:47.423 CAPropertyAnimationDemo[2829:99758] ****动画执行后:****{50, 50}** */

从上面的例子得出:即使保持了图层在执行完动画后的状态,但是实质上图层的属性值还是动画执行前的初始值,并没有真正被改动。如上例中的position
CABasicAnimation 还有一组重要的属性:autoreverses 和 repeatCount 。autoreverses 是否自动逆向执行动画到原先状态,执行时间与正向动画持续时间一致。repeatCount 是否重复动画。这两个属性默认值是NO。
平移、缩放、旋转
#import "ViewController.h" @interface ViewController ()@property (nonatomic,strong) CALayer *mylayer; @end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //1.创建一个layer CALayer *mylayer = [CALayer layer]; //2.设置layer属性 mylayer.bounds = CGRectMake(0, 0, 50, 80); mylayer.backgroundColor = [UIColor yellowColor].CGColor; mylayer.position = CGPointMake(50, 50); mylayer.anchorPoint = CGPointMake(0, 0); mylayer.cornerRadius = 20; //3.添加layer [self.view.layer addSublayer:mylayer]; self.mylayer = mylayer; }- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //[self keyPathWithPositionAnimation]; //[self keyPathWithBoundsAnimation]; //[self keyPathWithTransformAnimation]; [self positionByTransformAnimation]; }#pragma mark - 平移动画 - (void)keyPathWithPositionAnimation{ //1.创建核心动画(剧本) CABasicAnimation *anima = [CABasicAnimation animation]; //设置剧本具体动画内容 //设置layer的position属性,即平移 //注意:如果没有设置起始位置"fromValue"的值,那么会从当前位置开始 anima.keyPath = @"position"; anima.fromValue = https://www.it610.com/article/[NSValue valueWithCGPoint:CGPointMake(0, 0)]; anima.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)]; anima.duration = 2; //设置动画执行完毕之后不删除动画 anima.removedOnCompletion = NO; //设置保存动画的最新状态 anima.fillMode = kCAFillModeForwards; anima.delegate = self; NSString *string = NSStringFromCGRect(self.mylayer.frame); NSLog(@"动画执行前:%@",string); //2.添加核心动画到layer(执行剧本) [self.mylayer addAnimation:anima forKey:nil]; }#pragma mark - 缩放 - (void)keyPathWithBoundsAnimation{ //1.创建动画 CABasicAnimation *anima = [CABasicAnimation animation]; anima.keyPath = @"bounds"; //1.1设置动画执行时间 anima.duration = 2.0; //1.2设置保存动画的最新状态 anima.removedOnCompletion = NO; anima.fillMode = kCAFillModeForwards; //1.3修改属性,执行动画 anima.toValue = https://www.it610.com/article/[NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)]; //2.0添加动画到layer [self.mylayer addAnimation:anima forKey:nil]; }#pragma mark - 旋转 - (void)keyPathWithTransformAnimation{ CABasicAnimation *anima = [CABasicAnimation animation]; anima.keyPath = @"transform"; anima.duration = 2.0; anima.removedOnCompletion = NO; anima.fillMode = kCAFillModeForwards; anima.toValue = https://www.it610.com/article/[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2+M_PI_4, 1, 1, 0)]; [self.mylayer addAnimation:anima forKey:nil]; }#pragma mark - 平移的第二种方式 - (void)positionByTransformAnimation{ CABasicAnimation *anima = [CABasicAnimation animation]; anima.keyPath = @"transform"; anima.duration = 2.0; anima.removedOnCompletion = NO; anima.fillMode = kCAFillModeForwards; anima.toValue = https://www.it610.com/article/[NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, 100, 1)]; [self.mylayer addAnimation:anima forKey:nil]; }- (void)animationDidStart:(CAAnimation *)anim{ NSLog(@"动画开始执行了"); }- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{ NSString *string = NSStringFromCGRect(self.mylayer.frame); NSLog(@"动画执行后:%@",string); }

完整Demo

相关内容

热门资讯

大吉订购APP、优品订购APP...   大吉订购软件宣传的挺好,只要小资金就可以进行投资,并且收益还行,不少投资人就信以为真,觉得自己充...
琼海订购APP投资者持续亏损爆...   琼海订购APP白银铂金贵金属期货投资遭遇骗局,本金持续亏损,被爆仓!受害者该如何维护自身的权益?...
天天白银APP现货白银订购投资...   天天白银APP在抖音、快手、番茄小说等平台宣传:“白银即将暴涨”“五元起投”“24小时改变账户余...
掌上白银APP欺骗投资者,非法...   掌上白银APP(上海华通白银国际交易中心)欺骗投资者,投资者做白银铂金现货订购被骗!自带百倍杠杆...
天龙白银APP非法期货交易投资...   天龙白银APP广告宣传语中充斥着“5元即可投资,小资金,高回报”“在家也能操作赚钱”等诱导性的宣...
华通白银APP白银现货订购非实...   华通白银APP诱导投资者充值进行白银铂金订购交易,将现货白银订购弄成“期货交易”,可以买涨买跌,...
超... 本文目录导航: 超级云计算是什么 怎么做难看的PPT 1、...
谢... 本文目录导航: 请问云主机是什么 云主机有什么好处 具体的教程,谢谢! 云...
w... 本文目录导航: wps是什么意思 ppt的新配置designer和morp...
大... 本文目录导航: 大专学什么专业务工率高? 未来十年务工率最高的几大专业都是...
软... 本文目录导航: 软件技术专升本可以报什么专业 云计算专升本可以报医学吗 ...
云... 本文目录导航: 云计算务工前景 云计算务工方向及前景怎样样 ...
学... 本文目录导航: 学云计算进去无能嘛 云计算技术与运行是干什么的 ...
中... 本文目录导航: 如何了解云计算,中国的云计算产业开展现状如何 云计算未来几...
云... 本文目录导航: 云计算1+x证书含金量 云计算须要考什么证书 ...
云... 本文目录导航: 云计算股票龙头股票有哪些? 普通云计算概念龙头股有哪些?...
大... 本文目录导航: 大专云计算技术运行务工方向 大专毕业证上是物联网,实践学习...
大... 本文目录导航: 大数据云计算有必要升本吗 内蒙古大专云计算技术与运行专业升...
9... 本文目录导航: 99%学霸假期逆袭必看网站 99%学霸假期逆袭必看网站 ...
云... 本文目录导航: 云计算属于哪个专业 云计算属于什么专业 计...