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、transform103 /*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 }
模擬器效果截圖:
方法一效果圖:

方法二效果圖:

方法三效果圖:

新聞熱點
疑難解答