iPhone UILabel动画

Cha*_*sha 1 iphone ipad

我有一个UILabel,当标签中的值发生变化时,我想从另一种颜色突出显示它的背景颜色并存在2,3秒并恢复正常颜色.

任何人都知道如何做到这一点?

Ron*_*bro 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];
     }
    
    Run Code Online (Sandbox Code Playgroud)