一、核心动画
设置动画代理对象,当动画开始或者结束时会发消息给代理对象
当动画即将开始时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector
当动画结束时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector
动画的持续时间,秒为单位
动画延迟delay秒后再开始
动画的开始时间,默认为now
动画的节奏控制,具体看下面的”备注”
动画的重复次数
如果设置为YES,代表动画每次重复执行的效果会跟上一次相反
设置视图view的过渡效果, transition指定过渡类型, cache设置YES代表使用视图缓存,性能较好
十、Block动画
参数解析:
参数解析:
方法调用完毕后,相当于执行了下面两句代码:
// 添加toView到父视图
[fromView.superview addSubview:toView];
// 把fromView从父视图中移除
[fromView.superview removeFromSuperview];
参数解析:
十二、UIImageView的帧动画
十三、transform的属性列表
代码:
1 // 2 // ViewController.m 3 // IOS_0223_核心动画CoreAnimation 4 // 5 // Created by ma c on 16/2/23. 6 // Copyright © 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import <QuartzCore/QuartzCore.h> 11 12 @interface ViewController () 13 @property (weak, nonatomic) IBOutlet UIImageView *imgView; 14 15 @property (weak, nonatomic) IBOutlet UIView *greenView; 16 17 @property (nonatomic, strong) CALayer *layer; 18 19 @property (nonatomic, assign) BOOL isShake; 20 21 @property (nonatomic, assign) int index; 22 23 24 25 @end 26 27 @implementation ViewController 28 29 - (void)viewDidLoad { 30 [super viewDidLoad]; 31 32 //[self createLayer]; 33 34 } 35 ///创建图层 36 - (void)createLayer 37 { 38 CALayer *layer = [CALayer layer]; 39 layer.bounds = CGRectMake(0, 0, 100, 100); 40 41 layer.position = CGPointMake(self.view.center.x-50, 200); 42 layer.anchorPoint = CGPointZero; 43 layer.backgroundColor = [UIColor redColor].CGColor; 44 [self.view.layer addSublayer:layer]; 45 46 self.layer = layer; 47 } 48 49 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 50 { 51 // [self createTranslateAnimation]; 52 // [self createScaleAnimation]; 53 // [self createTransformAnimation]; 54 // [self createMoveAnimation]; 55 // [self createPathAnimation]; 56 // [self iconShake]; 57 // [self createTransitonAnimation]; 58 // [self createGroupAnimation]; 59 [self createViewAnimation]; 60 61 62 } 63 #pragma mark - UIView封装的动画 64 - (void)createViewAnimation 65 { 66 // 1. 67 // [UIView beginAnimations:nil context:nil]; 68 // self.greenView.center = CGPointMake(200, 200); 69 // //监听动画(动画执行完毕后,自动执行animateStop的方法) 70 // [UIView setAnimationDelegate:self]; 71 // [UIView setAnimationDidStopSelector:@selector(animateStop)]; 72 // [UIView commitAnimations]; 73 74 // 2. 75 // [UIView animateWithDuration:1.0 animations:^{ 76 // self.greenView.center = CGPointMake(200, 200); 77 // }completion:^(BOOL finished) { 78 // //动画执行完毕后执行 79 // }]; 80 81 // 3. 82 self.index++; 83 if (self.index == 9) { 84 self.index = 0; 85 } 86 NSString *fileName = [NSString stringWithFormat:@"%d.jpg",self.index]; 87 self.imgView.image = [UIImage imageNamed:fileName]; 88 89 [UIView transitionWithView:self.imgView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{ 90 ; 91 } completion:^(BOOL finished) { 92 ; 93 }]; 94 } 95 96 #pragma mark - 动画组 97 - (void)createGroupAnimation 98 { 99 CAAnimationGroup *group = [CAAnimationGroup animation]; 100 101 // 1.创建缩放动画对象 102 CABasicAnimation *scale = [CABasicAnimation animation]; 103 scale.keyPath = @"transform.scale"; 104 scale.toValue = @(0.0); 105 106 // 2.创建旋转动画对象 107 CABasicAnimation *rotate = [CABasicAnimation animation]; 108 rotate.keyPath = @"transform.rotation"; 109 rotate.toValue = @(M_PI); 110 111 group.animations = @[scale,rotate]; 112 group.duration = 2.0; 113 114 group.removedOnCompletion = NO; 115 group.fillMode = kCAFillModeForwards; 116 117 // 3.添加动画 118 [self.greenView.layer addAnimation:group forKey:nil]; 119 120 121 } 122 123 #pragma mark - 过渡动画 124 - (void)createTransitonAnimation 125 { 126 self.index++; 127 if (self.index == 9) { 128 self.index = 0; 129 } 130 NSString *fileName = [NSString stringWithFormat:@"%d.jpg",self.index]; 131 self.imgView.image = [UIImage imageNamed:fileName]; 132 133 //转场动画 134 CATransition *animation = [CATransition animation]; 135 animation.type = @"cube"; 136 animation.subtype = kCATransitionFromRight; 137 138 animation.startProgress = 0.2; 139 animation.endProgress = 0.6; 140 141 animation.duration = 0.5; 142 [self.imgView.layer addAnimation:animation forKey:nil]; 143 } 144 145 #pragma mark - 帧动画 146 ///图标抖动 147 - (void)iconShake 148 { 149 if (self.isShake) { 150 CAKeyframeAnimation *animation = [CAKeyframeAnimation animation]; 151 animation.keyPath = @"transform.rotation"; 152 153 animation.values = @[@(-M_PI_4 / 4),@(M_PI_4 / 4),@(-M_PI_4 / 4)]; 154 animation.duration = 0.25; 155 animation.repeatCount = MAXFLOAT; 156 157 animation.removedOnCompletion = NO; 158 animation.fillMode = kCAFillModeForwards; 159 160 [self.greenView.layer addAnimation:animation forKey:@"shake"]; 161 162 self.isShake = NO; 163 } 164 else 165 { 166 [self.greenView.layer removeAnimationForKey:@"shake"]; 167 self.isShake = YES; 168 } 169 } 170 171 - (void)createPathAnimation 172 { 173 CAKeyframeAnimation *animation = [CAKeyframeAnimation animation]; 174 animation.keyPath = @"position"; 175 176 //设置动画执行路径 177 CGMutablePathRef path = CGPathCreateMutable(); 178 CGPathAddEllipseInRect(path, NULL, CGRectMake(100, 100, 200, 200)); 179 animation.path = path; 180 CGPathRelease(path); 181 182 //设置动画执行节奏 183 animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; 184 185 //设置动画代理 186 animation.delegate = self; 187 188 animation.duration = 3.0; 189 190 animation.removedOnCompletion = NO; 191 animation.fillMode = kCAFillModeForwards; 192 193 [self.greenView.layer addAnimation:animation forKey:nil]; 194 } 195 196 #pragma mark - 动画代理 197 - (void)animationDidStart:(CAAnimation *)anim 198 { 199 NSLog(@"animationDidStart"); 200 } 201 202 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag 203 { 204 NSLog(@"animationDidStop"); 205 } 206 207 - (void)createMoveAnimation 208 { 209 CAKeyframeAnimation *animation = [CAKeyframeAnimation animation]; 210 animation.keyPath = @"position"; 211 212 NSValue *v1 = [NSValue valueWithCGPoint:CGPointMake(50, 200)]; 213 NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(200, 200)]; 214 NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(200, 400)]; 215 NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(50, 400)]; 216 animation.values = @[v1,v2,v3,v4]; 217 animation.duration = 3.0; 218 219 // animation.keyTimes = @[@(1),@(1.5),@(2)]; 220 221 animation.removedOnCompletion = NO; 222 animation.fillMode = kCAFillModeForwards; 223 224 [self.greenView.layer addAnimation:animation forKey:nil]; 225 } 226 227 #pragma mark - 基础动画 228 /*只能在两个值之间变化:fromValue -> toValue*/ 229 230 /// 动画 231 - (void)createTransformAnimation 232 { 233 // 1.创建动画对象 234 CABasicAnimation *animation = [CABasicAnimation animation]; 235 236 // 2.设置动画对象 237 // animation.keyPath = @"transform"; 238 // animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1, 1, 0)]; 239 240 // animation.keyPath = @"transform.rotation"; 241 // animation.toValue = @(M_PI); 242 243 // animation.keyPath = @"transform.scale"; 244 // animation.toValue = @(1.5); 245 246 animation.keyPath = @"transform.translation"; 247 animation.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)]; 248 249 animation.duration = 3.0; 250 251 animation.removedOnCompletion = NO; 252 animation.fillMode = kCAFillModeForwards; 253 254 // 3.添加动画 255 [self.layer addAnimation:animation forKey:nil]; 256 257 } 258 259 ///缩放动画 260 - (void)createScaleAnimation 261 { 262 // 1.创建动画对象 263 CABasicAnimation *animation = [CABasicAnimation animation]; 264 265 // 2.设置动画对象 266 animation.keyPath = @"bounds"; 267 animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)]; 268 animation.duration = 3.0; 269 270 animation.removedOnCompletion = NO; 271 animation.fillMode = kCAFillModeForwards; 272 273 // 3.添加动画 274 [self.layer addAnimation:animation forKey:nil]; 275 276 277 } 278 ///平移动画 279 - (void)createTranslateAnimation 280 { 281 // 1.创建动画对象 282 CABasicAnimation *animation = [CABasicAnimation animation]; 283 284 // 2.设置动画对象 285 //keyPath决定了执行哪个动画,调整哪个layer属性来执行动画 286 animation.keyPath = @"position"; 287 // animation.fromValue =[NSValue valueWithCGPoint:CGPointZero]; 288 //toValue:变成什么值 289 // animation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)]; 290 //byValue:增加多少值 291 animation.byValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)]; 292 animation.duration = 3.0; 293 294 //设置动画结束时,不要删除动画 295 animation.removedOnCompletion = NO; 296 //保持最新状态 297 animation.fillMode = kCAFillModeForwards; 298 299 // 3.添加动画 300 [self.layer addAnimation:animation forKey:nil]; 301 302 } 303 @end
原文:http://www.cnblogs.com/oc-bowen/p/5210969.html