CoreAnimation 核心动画一 (一些常用属性 和 方法)
1、常用属性:
frame bounds center alpha Transition 过渡 transform 动画效果
2、常用方法:
+(void)setAnimationDelegate:(id)delegate;
+(void)setAnimationWillStartSelector:(SEL)selector; 当动画结束的时候,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector
+ (void)setAnimationDidStopSelector:(SEL)selector; 当动画结束时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector
+ (void)setAnimationDuration:(NSTimeInterval)duration; 动画的持续时间,秒为单位
+ (void)setAnimationDelay:(NSTimeInterval)delay; 动画延迟delay秒后再开始
+ (void)setAnimationStartDate:(NSDate *)startDate; 动画的开始时间,默认为now
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve; 动画的节奏控制(过渡)
+ (void)setAnimationRepeatCount:(float)repeatCount; 动画的重复次数
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses; 如果设置为YES,代表动画每次重复执行的效果会跟上一次相反
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache; 设置视图view的过渡效果, transition指定过渡类型, cache设置YES代表使用视图缓存,性能较好
1 #import "ViewController.h" 2 3 @interface ViewController () 4 { 5 UIImageView *imageView; 6 } 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 14 imageView = [[UIImageView alloc]initWithFrame:self.view.frame]; 15 imageView.image = [UIImage imageNamed:@"7.jpg"]; 16 // imageView.contentMode = UIViewContentModeScaleAspectFit; 17 [self.view addSubview:imageView]; 18 19 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 20 button.frame = CGRectMake(0, 0, 100, 40); 21 button.center = self.view.center; 22 button.backgroundColor = [UIColor brownColor]; 23 [button addTarget:self action:@selector(viewAnimation3) forControlEvents:UIControlEventTouchUpInside]; 24 [self.view addSubview:button]; 25 26 27 } 28 29 30 #pragma mark -- 1、frame bounds center alpha --- 31 - (void)viewAnimation1 { 32 #pragma mark - 方法1 33 // 在一段时间内 执行完命令 34 // [UIView animateWithDuration:3 animations:^{ 35 // imageView.alpha = 0.5; 36 // }]; 37 38 #pragma mark - 方法2 39 // 开始动画 40 [UIView beginAnimations:@"animation" context:nil]; 41 42 // 这只动画的持续时间 43 [UIView setAnimationDuration:3]; 44 45 // ..... 动画效果 46 imageView.alpha = 0.5; 47 imageView.bounds = CGRectMake(0, 0, 100, 100); 48 imageView.center = CGPointMake(50, 200); 49 // imageView.frame = CGRectMake(100, 100, 100, 100); 50 51 // 提交动画 会去执行动画 52 [UIView commitAnimations]; 53 } 54 55 #pragma mark - 2、Transition 56 /* 57 typedef NS_ENUM(NSInteger, UIViewAnimationTransition) { 58 UIViewAnimationTransitionNone, 59 UIViewAnimationTransitionFlipFromLeft, 从左面翻转 60 UIViewAnimationTransitionFlipFromRight,从右面翻转 61 UIViewAnimationTransitionCurlUp, 向上翻页 62 UIViewAnimationTransitionCurlDown,向下翻页 63 }; 64 */ 65 - (void)viewAnimation2 { 66 // 开始动画 67 [UIView beginAnimations:nil context:nil]; 68 // 设置动画持续时间 69 // [UIView setAnimationDuration:3]; 70 // 设置 UIView 的过渡动画 71 [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:imageView cache:YES]; 72 73 #pragma mark - 3、UIViewAnimationCurve 74 /* 75 typedef NS_ENUM(NSInteger, UIViewAnimationCurve) { 76 UIViewAnimationCurveEaseInOut, // slow at beginning and end 慢进慢出 77 UIViewAnimationCurveEaseIn, // slow at beginning 快进 78 UIViewAnimationCurveEaseOut, // slow at end 快出 79 UIViewAnimationCurveLinear 匀速 80 }; 81 */ 82 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 83 // 设置 代理(检测动画结束) 84 [UIView setAnimationDelegate:self]; 85 [UIView setAnimationDidStopSelector:@selector(finishAnimation)]; 86 // 提交动画 87 [UIView commitAnimations]; 88 89 } 90 91 // 动画结束之后触发的方法 92 - (void)finishAnimation { 93 [UIView beginAnimations:@"o" context:nil]; 94 [UIView setAnimationDuration:3]; 95 imageView.alpha = 0.1; 96 imageView.bounds = CGRectZero; 97 imageView.center = CGPointMake(100, 100); 98 99 [UIView commitAnimations]; 100 } 101 102 #pragma mark - 4、transform 103 /* 104 imageView.transform=CGAffineTransformScale(imageView.transform, 0.5, 0.5); // 实现的是放大和缩小imageView.transform=CGAffineTransformRotate(imageView.transform, M_PI_4); //实现的是旋转 imageView.transform=CGAffineTransformTranslate(imageView.transform, 20, 0); //实现的是平移 105 */ 106 - (void)viewAnimation3 { 107 108 [UIView beginAnimations:@"animation" context:nil]; 109 [UIView setAnimationDuration:3]; 110 // transform 如果没有还原transform 他会保持 改变后的模样 111 imageView.transform = CGAffineTransformScale(imageView.transform, 0.5, 0.5); 112 113 [UIView setAnimationDelegate:self]; 114 [UIView setAnimationDidStopSelector:@selector(restore)]; 115 [UIView commitAnimations]; 116 117 } 118 119 - (void)restore { 120 [UIView animateWithDuration:3 animations:^{ 121 imageView.transform = CGAffineTransformIdentity; 122 123 }]; 124 }
模拟器效果截图:
方法一效果图:
方法二效果图:
方法三效果图: