I want a label to be shown for 0.4 seconds and then hidden for 0.8 seconds - in an infinite loop.
How can I pull that off?
I want a label to be shown for 0.4 seconds and then hidden for 0.8 seconds - in an infinite loop.
How can I pull that off?
我会说使用 NSTimer。您可以通过以下方式进行操作:
说你的标签是myLabel
:
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
您应该创建一个由以下方法调用的方法NSTimer
:
- (void)changeLabelState:(NSTimer *)timer
{
if(self.myLabel.hidden == TRUE)
{
self.myLabel.hidden = FALSE; //change comparassion to assing
[NSTimer scheduledTimerWithTimeInterval:0.4
target:self
selector:@selector(changeLabelState:)
userInfo:nil
repeats:NO];
}
else
{
self.myLabel.hidden = TRUE;
[NSTimer scheduledTimerWithTimeInterval:0.8
target:self
selector:@selector(changeLabelState:)
userInfo:nil
repeats:NO];
}
}
并像这样初始化NSTimer
某处:
[NSTimer scheduledTimerWithTimeInterval:0.4
target:self
selector:@selector(changeLabelState:)
userInfo:nil
repeats:NO];
请注意,您还可以执行以下操作:
[self performSelector:@selector(changeLabelState:) withObject:nil afterDelay:0.4];
- (void)changeLabelState:(NSTimer *)timer
{
if(self.myLabel.hidden == TRUE)
{
self.myLabel.hidden = FALSE;
[self performSelector:@selector(changeLabelState:) withObject:nil afterDelay:0.4];
}
else
{
self.myLabel.hidden = TRUE;
[self performSelector:@selector(changeLabelState:) withObject:nil afterDelay:0.8];
}
}
像下面这样的东西。
在 viewDidLoad 中:
NSTimer *silly = [NSTimer timerWithTimeInterval:0.4 target:self selector:@selector(question) userInfo:nil repeats:YES];
功能
-(void)question {
if(label.isHidden){
label.hidden = false;
} else {
label.hidden = true;
}
}
确保你在这个函数的范围内定义了一个 UILabel 并且它应该可以工作。未经测试。