【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

相关内容

热门资讯

想... 本文目录导航: 想问PLM系统怎样选用?是国产好还是国外的好呢? PLM是...
请... 本文目录导航: 请问PLM是干什么用的? plm是什么意思 ...
各... 本文目录导航: 各位能说一下PDM与PLM区别重要是什么? 目前 PDM ...
p... 本文目录导航: plm是什么系统 什么是PLM系统 plm...
p... 本文目录导航: plm是什么意思 plm是什么意思? PL...
在... 本文目录导航: 美云智数PLM系统有人用过吗?在产品翻新上,PLM长处是什么? ...
p... 本文目录导航: plm是什么意思怎样了解plm的意思 plm是什么意思 ...
p... 本文目录导航: 恳求各位大神,plm系统蕴含哪些模块或许软件?plm和pdm用繁复深刻的解...
p... 本文目录导航: plm系统关键干什么的 国际外最顶级的10款plm名目治理...
什... 本文目录导航: 什么是plm系统? plm系统是什么意思 plm系统的解释...
m... 本文目录导航: mes系统是什么 mes系统是什么 mes...
m... 本文目录导航: mesh什么意思 mesh 是什么意思? ...
m... 本文目录导航: mesugaki游戏怎样下载 和荧玩猜拳的游戏 me...
M... 本文目录导航: MES是什么意思? 放开mes是什么意思? ...
m... 本文目录导航: mes工程师和java工程师对今后技术优化那个更好 怎么才...
龙... 本文目录导航: 龙岗坪山比亚迪招工吗?有些什么岗位呢? mes测试工程师面...
i... 本文目录导航: information与message有什么区别? mes...
M... 本文目录导航: MES是什么? 一、什么是MES? erp...
e... 本文目录导航: erp有哪些模块 erp蕴含哪些内容有哪些内容 ...
领... 本文目录导航: 领星ERP性能上新 | 你关心的目的治理、业绩智能推送等新性能都在这里! ...