2013-02-25 96 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]; 
    } 
}