tags:

views:

75

answers:

1

Hi all,

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?

+2  A: 
  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];
     }
    
Ron Srebro
Hi Ron, Thanks alot. I'm going to try it now.
charith
Hi Ron, I have tried this with a button. ( do the animation When the button click) . But nothing happens. What may be the issue?
charith
You can't change the color of a UIButton with the type UIButtonRoundRect, hence you can't animate the color change.If you need a button you need to create a UIButton with style UIButtonStyleCustom and create it's looks.
Ron Srebro
No no. I meant I did it for a UILabel, when I click on a UIButton. Sorry for miscommunication. Still no luck. Also how I detect the text change event?
charith
I'm not sure I understand but I updated the answer in a way that might answer your question.
Ron Srebro
GREAT. your answer works perfect for me. Thanks...
charith