2010-07-30 72 views
0

我想創建一個顯示動畫圖像的按鈕。我在this的帖子中讀到了這個建議,我添加了一個UIImageView(帶有一個animationImages數組)作爲UIButton的子視圖。我試圖做到這一點,像這樣: (動畫1是在頭部聲明一個UIImageView對象)用UIImageView子視圖顯示動畫圖像的UIButton

- (void)viewDidLoad { 
UIImage *image1 = [UIImage imageNamed:@"Good1.png"]; 
UIImage *image2 = [UIImage imageNamed:@"Good2.png"]; 
UIImage *image3 = [UIImage imageNamed:@"Bad1.png"]; 
NSArray *images = [[NSArray alloc] initWithObjects:image1, image2, image3, nil]; 
animation1.animationImages = images; 
[images release]; 
[self.button1 addSubview:animation1]; 
[self.button1 bringSubviewToFront:animation1]; 
[button1 setNeedsDisplay]; 
animation1.animationDuration = 1; 
animation1.animationRepeatCount = 3; 
[animation1 startAnimating]; 
    [super viewDidLoad]; 
} 

但沒有出現在按鈕中。任何想法,我可能會出錯?

此外,我希望它可以根據用戶按下按鈕時顯示的圖像調用不同的方法。我之前實現了這一點,但創建了一個圖像數組,使用NSTimer將UIButton的背景圖像設置爲遍歷這些圖像,然後將按鈕的圖像數據與數組中的圖像進行比較,調用另一個圖像每個方法。這看起來效率不高!因此,我試圖在子視圖中循環顯示圖像。任何關於如何從按鈕被按下時UIButton的UIImageView子視圖中提取圖像數據的建議將不勝感激:)

謝謝!

邁克爾

回答

3

這純粹是猜測,但有時需要明確設置框架。 (例如,向UIBarButtonItem添加自定義按鈕時)嘗試添加一條線

animation1.frame = CGRectMake(0,0,50,50); 
+0

對不起,響應緩慢 - 我會給它一個去 - 謝謝! 有關如何提取UIImageView在按下按鈕時顯示的UIImage的任何想法? 乾杯:) – Smikey 2010-08-03 18:10:01