views:

224

answers:

2

Hello, I want a blinking button. Actually my button look like this:

[redButton setImage:[UIImage imageNamed:@"Button1.png"] forState: UIControlStateNormal];
[redButton setImage:[UIImage imageNamed:@"ButtonPressed.png"] forState: UIControlStateHighlighted];

Now I want to change the Buttonpicture in the normal State every second from Button1.png to Button2.png and back to Button1.png and so on... How can I do this?

Thanks for your help and sorry for my bad English.

A: 

Is is not possible to use a animated GIF?

Jose Conde
No because you can just use .png
Flocked
+2  A: 

You can change the image by scheduling a NSTimer:

NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval: 1.0
                                         target: self
                                       selector: @selector(toggleButtonImage:)
                                       userInfo: nil
                                        repeats: YES];  

The following method changes the picture (you also need a bool instance variable to keep your toggle state)

- (void)toggleButtonImage:(NSTimer*)timer
{
    if(toggle)
    {
        [test setImage:[UIImage imageNamed:@"Button1.png"] forState: UIControlStateNormal];
    }
    else 
    {
        [test setImage:[UIImage imageNamed:@"ButtonPressed.png"] forState: UIControlStateNormal];
    }
    toggle = !toggle;
}
weichsel