2010-08-01 85 views
1

我有一個UILabel,當標籤中的值發生變化時,我想從另一種顏色突出顯示它的背景顏色,並存在2,3秒,然後恢復正常顏色。iPhone UILabel動畫

任何人都有一個想法如何做到這一點?

+1

接受一些第一 – vodkhang 2010-08-01 14:46:32

+0

答案你是什麼意思? – 2010-08-01 15:12:28

回答

5
  1. 添加quartzCore作爲一個框架
  2. 添加進口QuartzCore/QuartzCore.h
  3. 使用此代碼

    - (void) initController 
    { 
         UIButton *myButton = [view viewWithTag:1]; // Just reference the button you have 
         [myButton addTarget:self action:@selector(animateLabel) forControlEvents:UIControlEventTouchUpInside]; 
    } 
    
    - (void) animateLabel 
    { 
    
        CABasicAnimation* highlightAnim = [CABasicAnimation animationWithKeyPath:@"backgroundColor"]; 
        highlightAnim.toValue = (id)[UIColor blueColor].CGColor; 
        highlightAnim.duration = 2;   // In seconds 
        highlightAnim.autoreverses = YES; // If you want to it to return to the normal color 
        [label.layer addAnimation:highlightAnim forKey:nil]; 
    } 
    
+0

嗨Ron, 非常感謝。我現在要試一試。 – 2010-08-01 14:23:29

+0

嗨羅恩,我試過這個按鈕。 (做動畫時點擊按鈕)。但沒有任何反應。可能是什麼問題? – 2010-08-01 14:55:31

+0

您不能使用UIButtonRoundRect類型更改UIButton的顏色,因此您無法爲顏色更改設置動畫效果。 如果你需要一個按鈕,你需要創建一個樣式爲UIButtonStyleCustom的UIButton並創建它的外觀。 – 2010-08-01 17:43:47