【问题标题】:How can I produce a blinking label如何制作闪烁的标签
【发布时间】:2013-02-25 16:44:59
【问题描述】:

我希望标签显示 0.4 秒,然后隐藏 0.8 秒 - 在无限循环中。

我怎样才能做到这一点?

【问题讨论】:

标签: objective-c xcode uilabel nstimer


【解决方案1】:

NSTimerUIViews hidden 属性是一种可能性

【讨论】:

  • 你的回答很好,请给出一些代码来支持这些关键字和类名。
  • 好问题 -> 好答案 |懒惰写的问题 -> 更笼统,不太棒的答案
【解决方案2】:

我会说使用 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];
    }
}

【讨论】:

    【解决方案3】:

    类似下面的东西。

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

    【讨论】:

    • 如果你想要不同的时间间隔,你可以考虑传递 repeats:NO 并在这个 question 方法中创建新的计时器(如果隐藏 -> 0.4 秒,如果不是 -> 0.8 秒)
    猜你喜欢
    • 1970-01-01
    • 2011-04-18
    • 2014-02-20
    • 2013-07-08
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 2013-03-27
    • 2015-01-30
    相关资源
    最近更新 更多