iOS開發UI篇—核心動畫(UIView封裝動畫)
一、UIView動畫(首尾)
1.簡單說明
UIKit直接将動畫內建到UIView類中,當内部的一些屬性發生改變時,UIView将為這些改變提供動畫支援
執行動畫所需要的工作由UIView類自動完成,但仍要在希望執行動畫時通知視圖,為此需要将改變屬性的代碼放在[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]之間
常見方法解析:
+ (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代表使用視圖緩存,性能較好
2.代碼示例
1 //
2 // YYViewController.m
3 // 01-uiview封裝動畫
4 //
5 // Created by apple on 14-6-22.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10
11 @interface YYViewController ()
12 @property (weak, nonatomic) IBOutlet UIView *customView;
13
14
15 @end
16
17 @implementation YYViewController
18
19 - (void)viewDidLoad
20 {
21 [super viewDidLoad];
22
23 }
24
25 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
26 {
27 //列印動畫塊的位置
28 NSLog(@"動畫執行之前的位置:%@",NSStringFromCGPoint(self.customView.center));
29
30 //首尾式動畫
31 [UIView beginAnimations:nil context:nil];
32 //執行動畫
33 //設定動畫執行時間
34 [UIView setAnimationDuration:2.0];
35 //設定代理
36 [UIView setAnimationDelegate:self];
37 //設定動畫執行完畢調用的事件
38 [UIView setAnimationDidStopSelector:@selector(didStopAnimation)];
39 self.customView.center=CGPointMake(200, 300);
40 [UIView commitAnimations];
41
42 }
43
44 -(void)didStopAnimation
45 {
46 NSLog(@"動畫執行完畢");
47 //列印動畫塊的位置
48 NSLog(@"動畫執行之後的位置:%@",NSStringFromCGPoint(self.customView.center));
49 }
50 @end
執行結果:

列印動畫塊的位置:
3.UIView封裝的動畫與CALayer動畫的對比
使用UIView和CALayer都能實作動畫效果,但是在真實的開發中,一般還是主要使用UIView封裝的動畫,而很少使用CALayer的動畫。
CALayer核心動畫與UIView動畫的差別:
UIView封裝的動畫執行完畢之後不會反彈。即如果是通過CALayer核心動畫改變layer的位置狀态,表面上看雖然已經改變了,但是實際上它的位置是沒有改變的。
代碼示例:
1 //
2 // YYViewController.m
3 // 01-uiview封裝動畫
4 //
5 // Created by apple on 14-6-22.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10
11 @interface YYViewController ()
12 @property (weak, nonatomic) IBOutlet UIView *customView;
13
14
15 @end
16
17 @implementation YYViewController
18
19
20
21 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
22 {
23 //1.建立核心動畫
24 CABasicAnimation *anima=[CABasicAnimation animation];
25 //平移
26 anima.keyPath=@"position";
27 //設定執行的動畫
28 anima.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 300)];
29
30 //設定執行動畫的時間
31 anima.duration=2.0;
32 //設定動畫執行完畢之後不删除動畫
33 anima.removedOnCompletion=NO;
34 //設定儲存動畫的最新狀态
35 anima.fillMode=kCAFillModeForwards;
36 // anima.fillMode=kCAFillModeBackwards;
37
38 //設定動畫的代理
39 anima.delegate=self;
40
41 //2.添加核心動畫
42 [self.customView.layer addAnimation:anima forKey:nil];
43 }
44
45 -(void)animationDidStart:(CAAnimation *)anim
46 {
47 //列印動畫塊的位置
48 // NSLog(@"動畫開始執行前的位置:%@",NSStringFromCGPoint(self.customView.center));
49 NSLog(@"動畫開始執行前的位置:%@",NSStringFromCGPoint( self.customView.layer.position));
50 }
51 -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
52 {
53 //列印動畫塊的位置
54 NSLog(@"動畫執行完畢後的位置:%@",NSStringFromCGPoint( self.customView.layer.position));
55 }
56
57 @end
列印結果:
二、block動畫
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
參數解析:
duration:動畫的持續時間
delay:動畫延遲delay秒後開始
options:動畫的節奏控制
animations:将改變視圖屬性的代碼放在這個block中
completion:動畫結束後,會自動調用這個block
轉場動畫
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
view:需要進行轉場動畫的視圖
options:轉場動畫的類型
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion
方法調用完畢後,相當于執行了下面兩句代碼:
// 添加toView到父視圖
[fromView.superview addSubview:toView];
// 把fromView從父視圖中移除
[fromView.superview removeFromSuperview];
1 #import "YYViewController.h"
2
3 @interface YYViewController ()
4 @property (weak, nonatomic) IBOutlet UIView *customView;
5
6 @end
7
8 @implementation YYViewController
9
10 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
11 {
12 //block代碼塊動畫
13 [UIView transitionWithView:self.customView duration:3.0 options:0 animations:^{
14 //執行的動畫
15 NSLog(@"動畫開始執行前的位置:%@",NSStringFromCGPoint(self.customView.center));
16 self.customView.center=CGPointMake(200, 300);
17 } completion:^(BOOL finished) {
18 //動畫執行完畢後的首位操作
19 NSLog(@"動畫執行完畢");
20 NSLog(@"動畫執行完畢後的位置:%@",NSStringFromCGPoint( self.customView.center));
21 }];
22 }
23 @end
提示:self.customView.layer.position和self.customView.center等價,因為position的預設值為(0.5,0.5)。
三、補充
1.UIImageView的幀動畫
UIImageView可以讓一系列的圖檔在特定的時間内按順序顯示
相關屬性解析:
animationImages:要顯示的圖檔(一個裝着UIImage的NSArray)
animationDuration:完整地顯示一次animationImages中的所有圖檔所需的時間
animationRepeatCount:動畫的執行次數(預設為0,代表無限循環)
相關方法解析:
- (void)startAnimating; 開始動畫
- (void)stopAnimating; 停止動畫
- (BOOL)isAnimating; 是否正在運作動畫
2.UIActivityIndicatorView
是一個旋轉進度輪,可以用來告知使用者有一個操作正在進行中,一般用initWithActivityIndicatorStyle初始化
方法解析:
UIActivityIndicatorViewStyle有3個值可供選擇:
UIActivityIndicatorViewStyleWhiteLarge //大型白色訓示器
UIActivityIndicatorViewStyleWhite //标準尺寸白色訓示器
UIActivityIndicatorViewStyleGray //灰色訓示器,用于白色背景