IOS開發實戰-----Tom貓,ios實戰-----tom

來源:互聯網
上載者:User

IOS開發實戰-----Tom貓,ios實戰-----tom

項目名稱:Tom貓

項目知識:UIImageView控制項動畫播放

:

 

 

開始項目

 

一、

(1)添加一個項目,在storyboard添加一個UIImageView

布局將真箇ViewControl布滿。

(2)添加UIImagView 預設 image,這張圖片為Tom平時狀態。

(3)開始寫後台代碼,選擇分頁模式 

 (4)Tom觸摸點分解

Tom貓點擊頭部,胸部,腳以及6個表徵圖都會做出相應的動作。其實這9個的核心代碼是一樣。這裡我僅僅寫頭部的代碼,其他部分留給讀者自己思考。

(6)在Tom頭部添加一個Button,當觸摸到這個按鈕的時候,就觸發頭部動作。

設定Button的屬性,使得Button的文本消失。

(7)進行連線,編寫處理代碼

在TouchHead_Click函數添加一下函數

    for(int i=0;i<80;i++)

    {

        //迴圈添加圖片

        NSString *imgName=[NSString stringWithFormat:@"knockout_%02d.jpg",i];

        NSString *path=[[NSBundle mainBundle] pathForResource:imgName ofType:nil];

        UIImage *img=[UIImage imageWithContentsOfFile:path];

        [imgArray addObject:img];

        [self LoadImageWithClearImage];

    }

 

另外需要自寫兩個函數,

1,-(void)ClearImage;清除圖片

2,-(void)LoadImageWithClearImage//載入圖片跟清楚圖片

具體如下

-(void)ClearImg

{

    self.ImageCat.image=nil;

    [imgArray removeAllObjects];

}

-(void)LoadImageWithClearImage

{

    //設定動畫源

    [self.ImageCat setAnimationImages:imgArray];

    //設定動畫重複次數

    [self.ImageCat setAnimationRepeatCount:1];

    //設定動畫播放的速度

    [self.ImageCat setAnimationDuration:imgArray.count*0.1];

    //設定動畫播放完的執行的函數

    [self.ImageCat performSelector:@selector(ClearImg) withObject:nil afterDelay:self.ImageCat.animationDuration];

    //開始動畫

    [self.ImageCat startAnimating];

}

到這裡核心代碼已經完全寫完,後面8個動作代碼基本一樣。

二、思路總結

定義了一個全域的NSMutableArray,作為圖片儲存數組。當我點擊按頭部的時候,會觸發載入動畫+清除動畫。

具體如下

    //i為圖片的張數

    for(int i=0;i<80;i++)

    {

        //迴圈添加圖片

        NSString *imgName=[NSString stringWithFormat:@"knockout_%02d.jpg",i];

        NSString *path=[[NSBundle mainBundle] pathForResource:imgName ofType:nil];

        UIImage *img=[UIImage imageWithContentsOfFile:path];

        [imgArray addObject:img];

    }

    [self LoadImageWithClearImage];

 

當我載入完圖片的時候就應該開始動畫。但是在開始動畫前需要動畫進行設定。

設定動畫的源-----設定動畫的重複次數---設定動畫的速度--設定動畫結束是執行的函數--設定開始動畫

具體代碼如下:

    //設定動畫源

    [self.ImageCat setAnimationImages:imgArray];

    //設定動畫重複次數

    [self.ImageCat setAnimationRepeatCount:1];

    //設定動畫播放的速度

    [self.ImageCat setAnimationDuration:imgArray.count*0.1];

    //設定動畫播放完的執行的函數

    [self.ImageCat performSelector:@selector(ClearImg) withObject:nil afterDelay:self.ImageCat.animationDuration];

    //開始動畫

    [self.ImageCat startAnimating];

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.