iOS動畫開發之二——UIView動畫執行的另外一種方式

iOS動畫開發之二——UIView動畫執行的另外一種方式

        上一篇博客中介紹了UIView的一些經常使用動畫,經過block塊,咱們能夠很方便簡潔的建立出動畫效果:http://my.oschina.net/u/2340880/blog/484457,這篇博客再介紹一種更加傳統的執行UIView的動畫的方法。編程

        這種方式相好比block的方式,顯得要麻煩一些,apple官方也推薦咱們使用帶block的建立動畫的方式,咱們能夠將編程重心更多的放在動畫邏輯的實現上。使用begin和commit方式主要分爲三個步驟:app

    1、設置動畫開始函數

[UIView beginAnimations:@"test" context:nil];

這個函數中的兩個參數,第一個用於設置一個動畫的標識id,一般第二個參數寫爲nil。動畫

    2、動畫執行的參數設置spa

 

+ (void)setAnimationDelegate:(id)delegate; .net

    設置這個動畫的代理,用於執行動畫開始或者結束後的動做3d

 

+ (void)setAnimationWillStartSelector:(SEL)selector; 代理

    設置動畫開始時執行的回調code

 

+ (void)setAnimationDidStopSelector:(SEL)selector;blog

    設置動畫結束後執行的回調

 

+ (void)setAnimationDuration:(NSTimeInterval)duration;

    設置動畫執行的時間

 

+ (void)setAnimationDelay:(NSTimeInterval)delay; 

    設置延時執行的延時

 

+ (void)setAnimationStartDate:(NSDate *)startDate; 

    給動畫設置一個啓示時間

 

+ (void)setAnimationCurve:(UIViewAnimationCurve)curve; 

    設置動畫播放的線性效果,UIViewAnimationCurve的枚舉以下:

typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
    UIViewAnimationCurveEaseInOut,         // 淡入淡出
    UIViewAnimationCurveEaseIn,            // 淡入
    UIViewAnimationCurveEaseOut,           // 淡出
    UIViewAnimationCurveLinear            //線性
}

 

+ (void)setAnimationRepeatCount:(float)repeatCount; 

    設置動畫循環播放次數

 

+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;

    設置動畫逆向執行

 

    3、提交動畫

+ (void)commitAnimations;

    例如:

UIView * view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    [self.view addSubview:view];
    view.backgroundColor=[UIColor redColor];
    [UIView beginAnimations:@"test" context:nil];
    [UIView setAnimationDuration:3];
    view.backgroundColor=[UIColor orangeColor];
    [UIView commitAnimations];//執行commit後,動畫即開始執行

 

一點建議:這種建立UIView動畫的方式和上一篇博客中的block方式效果相同,然而效率並不高,寫的代碼也會繁瑣冗長,在開發中,若是沒有特殊的兼容要求,使用block的方式會更高效方便。   

專一技術,熱愛生活,交流技術,也作朋友。

——琿少 QQ羣:203317592

相關文章
相關標籤/搜索