-3

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?

4

3 回答 3

2

NSTimerUIViews hidden属性是一种可能性

于 2013-02-25T16:48:44.977 回答
1

我会说使用 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];
    }
}
于 2013-02-25T17:07:35.617 回答
0

像下面这样的东西。

在 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 并且它应该可以工作。未经测试。

于 2013-02-25T16:56:28.307 回答