如何获得一个闪烁的按钮? (两张交替图片) [英] 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];
现在我想从Button1.png到Button2.png每秒更改正常状态下的Buttonpicture并回到Button1.png等等......我怎么能这样做?
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.
推荐答案
您可以通过安排NSTimer来更改图像:
You can change the image by scheduling a NSTimer:
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval: 1.0
target: self
selector: @selector(toggleButtonImage:)
userInfo: nil
repeats: YES];
以下方法更改图片(您还需要一个bool实例变量来保持切换状态)
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;
}
这篇关于如何获得一个闪烁的按钮? (两张交替图片)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文