【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打着现货交易幌子搞...   网络上各类白银、铂金等贵金属投资广告铺天盖地,不少平台以"现货交易"为名,暗中却从事着非法期货交...
5元起投!高额收益?中国白银、...   上海华通白银国际交易中心有限公司(以下简称“华通白银国际”)打着“白银、铂金现货订购”的旗号,实...
"九州订购APP、起...   近年来,贵金属投资热度持续攀升,但乱象也随之而来。一些不法分子趁机搭建虚假交易平台,打着"低门槛...
坚锅淘金APP投资者频繁爆仓亏...   坚锅淘金APP对外宣称是贵金属现货交易平台,日常出入金看似畅通无阻,给人一种合规运营的假象。但事...
斗金订购APP“5元订购现货”...   斗金订购APP“5元订购现货”骗局揭秘:充值易、回本难,越陷越深!  许多投资人在未核实平台合规...
贵金属APP乱象曝光:&quo...   当前市场上部分贵金属类APP未取得正规交易资质,涉嫌误导消费者参与白银、铂金等商品的非合规交易!...
华通白银、银宝街APP被指虚假...   警惕现货白银投资陷阱:上海华通白银背后的平台涉嫌非法期货交易  近年来,各类现货白银交易平台以“...
充值送300,月入过万?别被量...   如今,网络上充斥着大量未经批准的贵金属及其他产品订购交易平台。这些平台普遍缺乏合法经营资质,却将...
鑫汇智投APP贵金属非法期货、...   鑫汇智投APP贵金属非法期货、人为控盘,大量投资者遭恶意收割  鑫汇智投APP长期以“低门槛、高...
矅光智策APP非法贵金属交易骗...   不少投资人在矅光智策APP上进行贵金属交易后出现了严重亏损。许多人在下载该平台时,根本不清楚其是...
飞鸽订购APP非法期货交易骗局...   飞鸽订购APP非法期货交易骗局,很多投资者根本没搞懂规则就盲目充值,买涨买跌跟买cai票一样!平...
天龙白银APP被操控的非法期货...   中小投资者在网络平台参与商品现货订购交易,表面看似合规合法,实则暗藏诸多难以察觉的风险。商品现货...
下载送试玩金?千万别碰!带你揭...   当前,许多贵金属APP交易平台正采用欺诈性营销手段。它们通过抖音等短视频平台,以“5元起投”“充...
泡泡淘金APP骗局:打着&qu...   泡泡淘金APP骗局:打着"商品订购"幌子,实则非法期货收割投资者!投资需谨慎,切勿轻信“高额回报...
南商云购APP虚假电子盘背后的...   南商云购是虚假电子盘吗?它是否具备合规的交易资质?这个曾经的"现货电子盘"如今摇身一变,披上了"...
警惕"祥龙订购&qu...   祥龙订购APP、掌上银牛APP等平台背后的运营方——四川禄宏商品现货市场有限公司,仅具备现货交易...
中国白银、华银:虚假宣传、后台...   中国白银APP宣称是上市公司“中国白银集团”与“上海华通白银国际交易中心”旗下的现货交易平台,提...
起底“优品订购”APP非法期货...   “优品订购”APP根本不具备任何合法的交易资质。其背后的运营方仅凭随便注册一家公司、开发一款交易...