Charith Nidarsha
Charith Nidarsha

Reputation: 4245

iPhone UILabel animation

I have a UILabel and when the value change in the label, I want to highlight it's background color from another color and exists for 2,3 seconds and get back to normal color.

Anyone have an idea how to do this?

Upvotes: 1

Views: 1362

Answers (1)

Ron Srebro
Ron Srebro

Reputation: 6862

  1. Add quartzCore as a framework
  2. Add an import QuartzCore/QuartzCore.h
  3. Use this code

    - (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];
     }
    

Upvotes: 5

Related Questions