2013-02-25 231 views
-3

我想要标签显示0.4秒,然后隐藏0.8秒 - 无限循环。如何生成闪烁的标签

我该如何解决这个问题?

+5

(http://whathaveyoutried.com) – 2013-02-25 16:45:39

+1

Noooooooo !!!!!!! !!!! 2013-02-25 17:13:56

回答

2

NSTimerUIView小号hidden属性将是一个可能性

+0

你的回答很好,请给出一些代码来支持这些关键字和类名。 – 2013-02-25 16:50:53

+5

好问题 - >好答案| lazyly书面问题 - >更一般的,少真棒答案 – blub 2013-02-25 17:12:13

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,它应该工作。 UNTESTED。

+1

如果你愿意,你可以考虑通过'重复不同的时间间隔:NO'这'question'方法中创造新的定时器(如果隐藏 - >0.4秒,如果没有 - > 0.8秒) – blub 2013-02-25 17:08:18

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