IOS三種動畫方式

來源:互聯網
上載者:User

標籤:

IOS開發UI篇—iOS開發中三種簡單的動畫設定

 

一、首尾式動畫

程式碼範例:

    // beginAnimations表示此後的代碼要“參與到”動畫中    [UIView beginAnimations:nil context:nil];//設定動畫時間長度    [UIView setAnimationDuration:2.0];          self.headImageView.bounds = rect;    // commitAnimations,將beginAnimation之後的所有動畫提交並產生動畫    [UIView commitAnimations];

說明:如果只是修改控制項的屬性,使用首尾式動畫還是比較方便的,但是如果需要在動畫完成後做後續處理,就不是那麼方便了

二、block代碼塊動畫

程式碼範例:

   //簡單的動畫效果    [UIView animateWithDuration:2.0 animations:^{        showlab.alpha=0;    } completion:^(BOOL finished) {        [showlab removeFromSuperview];    }];

說明:

(1)在實際的開發中更常用的時block代碼塊來處理動畫操作。

(2)塊動畫相對來說比較靈活,尤為重要的是能夠將動畫相關的代碼編寫在一起,便於代碼的閱讀和理解.

三、序列幀動畫(以一個簡單的TOM貓動畫樣本)

      

匯入提前準備好的素材,對UIImageview和button按鈕進行連線。

程式碼範例:

- (IBAction)eat {        NSMutableArray  *arrayM=[NSMutableArray array];    for (int i=0; i<40; i++) {        [arrayM addObject:[UIImage imageNamed:[NSString stringWithFormat:@"eat_%02d.jpg",i]]];    }    //設定動畫數組    [self.tom setAnimationImages:arrayM];    //設定動畫播放次數    [self.tom setAnimationRepeatCount:1];    //設定動畫播放時間    [self.tom setAnimationDuration:40*0.075];    //開始動畫    [self.tom startAnimating];}

點擊按鈕,即可執行動畫,實現效果如下:

四、補充知識

1. Images.xcassets中的素材

(1)只支援png格式的圖片

(2) 圖片只支援[UIImage imageNamed]的方式執行個體化,但是不能從Bundle中載入

(3)  在編譯時間,Images.xcassets中的所有檔案會被打包為Assets.car的檔案

 

 2. UIImageView的序列幀動畫(需要考慮程式效能,釋放資料)

// 0. 是否正在動畫

[self.tom isAnimating];

// 1. 設定圖片的數組

[self.tom setAnimationImages:arrayM];

// 2. 設定動畫時間長度,預設每秒播放30張圖片

[self.tom setAnimationDuration:arrayM.count * 0.075];

// 3. 設定動畫重複次數,預設為0,無限迴圈

[self.tom setAnimationRepeatCount:1];

// 4. 開始動畫

[self.tom startAnimating];

// 5. 動畫播放完成後,清空動畫數組

[self.tom performSelector:@selector(setAnimationImages:) withObject:nilafterDelay:self.tom.animationDuration];

 

3. UIImage imageNamed

(1)在圖片使用完成後,不會直接被釋放掉,具體釋放時間由系統決定,適用於圖片小,常用的影像處理

(2)如果要釋放快速釋放圖片,可以使用[UIImage imageWithContentsOfFile:path]執行個體化映像

 

4. 方法重構的策略

(1) 將具有共性的代碼複製到一個新的方法

(2)根據不同的調用情況,增加方法的參數

提示:在寫程式時不要著急重構,有時候把代碼先寫出來,更容易看清楚如何重構才會更好

 

5. Bundle(包)中的圖片素材

往項目中拖拽素材時,通常選擇

(1) Destination: 勾選

(2) Folders:

    1)選擇第一項:黃色檔案夾

        注意點:Xcode中分檔案夾,Bundle中所有所在都在同一個檔案夾下,因此,不能出現檔案重名的情況

        特點:

       a.可以直接使用[NSBundle mainBundle]作為資源路徑,效率高!

       b.可以使用[UIImage imageNamed:]載入映像

    2)選擇第二項:藍色檔案夾

        注意點:Xcode中分檔案夾,Bundle中同樣分檔案夾,因此,可以出現檔案重名的情況

        特點:

        a.需要在[NSBundle mainBundle]的基礎上拼接實際的路徑,效率較差

        b.不能使用[UIImage imageNamed:]載入圖

 

  

IOS三種動畫方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.