2009-10-28 74 views
3

我有幾個UIButtons,當我在屏幕中間按下其他不相關的東西時,我想要觸發短5-10幀動畫。我可以在IB中放置一個UIImageView並以編程方式更新它嗎?它不需要過於複雜,我甚至不介意它是否被硬編碼,因爲它是一個如此小的項目。簡單的動畫[iPhone]

謝謝。

回答

3

這裏是你做的,只需幾幀的動畫是最簡單的方法的例子:

聲明一個IBOutlet UIImageView *timerAnimation並將其鏈接到您的IB的UIImageView。爲您的資源添加一些圖片,例如您在下面看到的需要。然後將圖像加載到一個數組 - 看到代碼:

timerAnimation.animationImages = [NSArray arrayWithObjects: 
     [UIImage imageNamed:@"timer00.png"], 
     [UIImage imageNamed:@"timer01.png"], 
     [UIImage imageNamed:@"timer02.png"], 
     [UIImage imageNamed:@"timer03.png"], 
     [UIImage imageNamed:@"timer04.png"], 
     [UIImage imageNamed:@"timer05.png"], 
     [UIImage imageNamed:@"timer06.png"], 
     [UIImage imageNamed:@"timer07.png"], 
     [UIImage imageNamed:@"timer08.png"], 
     [UIImage imageNamed:@"timer09.png"], 
     [UIImage imageNamed:@"timer10.png"], 
     [UIImage imageNamed:@"timer11.png"], 
     [UIImage imageNamed:@"timer12.png"], 
     [UIImage imageNamed:@"timer13.png"], 
     [UIImage imageNamed:@"timer14.png"], 
     [UIImage imageNamed:@"timer15.png"], 
     [UIImage imageNamed:@"timer16.png"], 
     [UIImage imageNamed:@"timer17.png"], 
     [UIImage imageNamed:@"timer18.png"], 
     [UIImage imageNamed:@"timer19.png"], 
     [UIImage imageNamed:@"timer20.png"], 
     [UIImage imageNamed:@"timer21.png"], 
     [UIImage imageNamed:@"timer22.png"], 
     [UIImage imageNamed:@"timer23.png"], 
     [UIImage imageNamed:@"timer24.png"], 
     [UIImage imageNamed:@"timer25.png"], 
     [UIImage imageNamed:@"timer26.png"], 
     [UIImage imageNamed:@"timer27.png"], 
     [UIImage imageNamed:@"timer28.png"], 
     [UIImage imageNamed:@"timer29.png"], 
     [UIImage imageNamed:@"timer30.png"], 
     [UIImage imageNamed:@"timer31.png"], 
     [UIImage imageNamed:@"timer32.png"], 
     [UIImage imageNamed:@"timer33.png"], 
     [UIImage imageNamed:@"timer34.png"], 
     [UIImage imageNamed:@"timer35.png"], 
    //  [UIImage imageNamed:@"timer36.png"], save last picture for "times up" 
     nil]; 
timerAnimation.animationDuration = 5.0; 
timerAnimation.animationRepeatCount = 1; 
[timerAnimation startAnimating]; 

,直到調用[timerAnimation stopAnimating];

+0

YES YES YES YES這將運行你的動畫!這絕對正是我所需要的。不是太複雜,只是簡單到易於實現。 – nullArray 2009-10-28 16:37:07

0

是的。創建一個IBOutlet到你的UIImageView,你可以從代碼更新它。例如,使用其image屬性來設置應顯示的UIImage。或者,您可以將圖像添加到應用程序資源中,並將其與Interface Builder中的UIImageView相關聯。由於iPhone已經針對它們進行了優化,因此請使用.png文件。
由於UIImageView是UIView,因此可以使用所有常用的Core Animation methods