开发者

How to get a blinking button? (two alternating pictures)

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.


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


Is is not possible to use a animated GIF?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜