UI基础 - CAAnination:关键帧动画 | 基础动画 | 过渡动画
CAAnination
1 - CAAnination 类中封装了 iOS 中所有的动画效果,其动画都是添加在 CALayer 上的,是动画的触发核心,它是抽象父类,通常使用其子类实现动画效果
2 - 它的三个子类分别是:CAPropertyAnimation、CAAnimationGroup、CATransition。其中 CAPropertyAnimation 同样是抽象类,有两个子类 CABasicAnimation、CAKeyFrameAnimation,用来给 CALayer 部分属性添加动画
3 - 常用 API
① 给 layer 添加、移除动画
- (void)addAnimation:(CAAnimation *)anim forKey:(NSString *)key; - (void)removeAnimationForKey:(NSString *)key; - (void)removeAllAnimations;
② CABasicAnimation 通过设定初始值和结束值执行动画
+ (id)animationWithKeyPath:(NSString *)path; //系统提供的构造方法 @property(copy) NSString *keyPath; // 只能填写 CALayer 动画中能够用动画的属性名 @property(retain) id fromValue;// 起始值 @property(retain) id toValue; // 结束值 @property(retain) id byValue; // 相对值
③ CAKeyFrameAnimation:关键帧动画可以让你的 view 的 layer 按照预定轨迹做动画
+ (id)animationWithKeyPath:(NSString *)path;// 构造方法 @property CGPathRef path;// 动画路径 @property(copy) NSArray *values; // 提供了一组关键帧的值,当使用 path 的时候,values 会被忽略 @property(copy) NSArray *keyTimes;// 每一帧的时间,其成员必须是 NNSNumber @property(copy) NSString *rotationMode;// 设定关键帧中的值如何计算
④ CAAnimationGroup 只有一个数组属性:添加多个 CAAnimation 一起执行
⑤ CATransition:用作 layer 动画过渡,它有两个主要属性:type(动画过渡效果)和 subType(动画过渡方向)
4 - 代码示例
1 #import "ViewController.h" 2 @interface ViewController () 3 // 动画视图 4 @property(nonatomic,strong)UIView *animationView; 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 12 self.view.backgroundColor = [UIColor redColor]; 13 14 // animationView 15 self.animationView = [[UIView alloc] initWithFrame:CGRectMake(30, 50, 90, 80)]; 16 self.animationView.backgroundColor = [UIColor cyanColor]; 17 [self.view addSubview:self.animationView]; 18 // 初始化状态 19 self.animationView.layer.cornerRadius = 20; 20 self.animationView.layer.borderWidth = 5; 21 self.animationView.layer.borderColor = [UIColor yellowColor].CGColor; 22 self.animationView.layer.shadowOpacity = 1.0; 23 self.animationView.layer.shadowOffset = CGSizeMake(8, 8); // 阴影偏移量 24 self.animationView.layer.shadowColor = [UIColor greenColor].CGColor;// 阴影颜色 25 26 // 基本动画 27 // [self testBasicAnimation]; 28 29 // 关键帧动画 30 // [self testKeyFrameAnimation]; 31 32 // 过渡动画 33 // [self testTransitionAnimation]; 34 35 } 36 37 // 基本动画 38 -(void)testBasicAnimation{ 39 40 //---------------------------------CABasicAnimation 41 // // 效果一:控制 y 的值 42 // CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"]; 43 // basicAnimation.duration = 1.0; 44 // basicAnimation.repeatCount = 1; 45 // basicAnimation.autoreverses = YES; 46 // // 动画的起始值 47 // basicAnimation.fromValue = [NSNumber numberWithFloat:90]; 48 // // 动画的结束值 49 // basicAnimation.toValue = [NSNumber numberWithFloat:200]; 50 // // 添加动画 51 // [self.animationView.layer addAnimation:basicAnimation forKey:nil]; 52 53 54 // // 效果二:控制 position 55 // CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 56 // basicAnimation.duration = 1.0; 57 // basicAnimation.repeatCount = 1; 58 // basicAnimation.autoreverses = YES; 59 // basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(160, 90)]; 60 // basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)]; 61 // [self.animationView.layer addAnimation:basicAnimation forKey:nil]; 62 63 64 // // 效果三:颜色渐变 65 // CABasicAnimation *animationColor = [CABasicAnimation animationWithKeyPath:@"backgroundColor"]; 66 // animationColor.duration = 1.0; 67 // animationColor.repeatCount = 1; 68 // animationColor.autoreverses = YES; 69 // animationColor.fromValue = (id)[UIColor orangeColor].CGColor; 70 // animationColor.toValue = (id)[UIColor cyanColor].CGColor; 71 // [self.animationView.layer addAnimation:animationColor forKey:nil]; 72 73 74 //------------------------------------CAAnimationGroup 75 // 效果二:控制 position 76 CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 77 basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(160, 90)]; 78 basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)]; 79 80 // 效果三:颜色渐变 81 CABasicAnimation *animationColor = [CABasicAnimation animationWithKeyPath:@"backgroundColor"]; 82 animationColor.fromValue = (id)[UIColor orangeColor].CGColor; 83 animationColor.toValue = (id)[UIColor cyanColor].CGColor; 84 85 86 // 注:效果二、三,我们可以使用 CAAnimationGroup 进行优化 87 // 重复的属性有 duration、autoreverses、repeatCount 以及两个 addAnimation 88 CAAnimationGroup *group = [CAAnimationGroup animation]; 89 group.animations = @[basicAnimation,animationColor]; 90 // 重复的属性 91 group.duration = 2; 92 group.repeatCount = 2; 93 group.autoreverses = YES; 94 [self.animationView.layer addAnimation:group forKey:nil]; 95 96 } 97 98 99 // 关键帧动画 100 - (void)testKeyFrameAnimation{ 101 102 CAKeyframeAnimation * keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 103 // 指定 6 个帧数 104 keyFrameAnimation.values = @[[NSValue valueWithCGPoint:CGPointMake(50, 50)], 105 [NSValue valueWithCGPoint:CGPointMake(200, 400)], 106 [NSValue valueWithCGPoint:CGPointMake(180, 210)], 107 [NSValue valueWithCGPoint:CGPointMake(100, 300)], 108 [NSValue valueWithCGPoint:CGPointMake(50, 50)], 109 [NSValue valueWithCGPoint:CGPointMake(200, 400)]]; 110 111 keyFrameAnimation.duration = 15;// 默认每个动画时间 15.0 /(6.0-1.0) 112 // 每帧结束占总时长百分比: 113 keyFrameAnimation.keyTimes = @[@0,@0.1,@0.2,@0.65,@0.8,@1.0];// 每个间段所占比 = 后一个的比 - 前一个的比 114 keyFrameAnimation.repeatCount = 1; 115 [self.animationView.layer addAnimation:keyFrameAnimation forKey:nil]; 116 } 117 118 119 // 过渡动画 120 - (void)testTransitionAnimation{ 121 122 CATransition * transition = [CATransition animation]; 123 transition.type = @"moveIn"; 124 transition.duration = 2.0; 125 126 // 代理:监听开始和结束 127 // transition.delegate = self;// 此处不在演示,使用时引入头文件、实现代理方法即可 128 transition.subtype = kCATransitionFromRight; 129 // 注意,使用的是 self.view 130 [self.view.layer addAnimation:transition forKey:nil]; 131 132 /*----------- 动画效果 133 134 @"cube" 立方体翻滚效果 135 @"moveIn" 新视图移到旧视图上面 136 @"reveal" 显露效果(将旧视图移开,显示下面的新视图) 137 @"fade" 交叉淡化过渡(不支持过渡方向) (默认为此效果 138 @"pageCurl" 向上翻一页 139 @"pageUnCurl" 向下翻一页 140 @"suckEffect" 收缩效果,类似系统最小化窗口时的神奇效果(不支持过渡方向) 141 @"rippleEffect" 滴水效果,(不支持过渡方向) 142 @"oglFlip" 上下左右翻转效果 143 @"rotate" 旋转效果 144 @"push" 145 @"cameraIrisHollowOpen" 相机镜头打开效果(不支持过渡方向) 146 @"cameraIrisHollowClose" 相机镜头关上效果(不支持过渡方向) 147 */ 148 } 149 @end
运行效果:初始视图
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)