2013-02-08 141 views
0

我試圖創建一個記憶遊戲,但在某個時間點和時間,我想要一個UIbutton上的圖像被閃現。對於x秒的秒數,我希望它們是可見的,對於x秒的時間我希望它們被隱藏。我被卡住了,只是想讓別人給我一個可行的算法。謝謝。在UIButton上創建閃爍的圖像?

+0

請參閱http:// stackoverflow。com/questions/2823635 /我怎麼能使一個uibutton閃光與輝光或變化它的形象爲spli – 2013-02-08 14:23:19

回答

2

基本方法是通過設置alpha來彈出視圖。

UIView *view = imageView; // Or whatever 
NSTimeInterval x = 2.0; // Or whatever 

double delayInSeconds = x; 
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); 

dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ // Wait for x seconds to hide 
    view.alpha = 0.0; // HIDE 

    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ // Wait for x seconds to show 
     view.alpha = 1.0; // SHOW 
    }); 
}); 

稍微視覺上更吸引人的方法是在一個短的持續時間褪色的圖像視圖和縮小。

UIView *view = imageView; // Or whatever 
NSTimeInterval x = 2.0; // Or whatever 
NSTimeInterval fadeInterval = 0.5; // Or whatever 

double delayInSeconds = x; 
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); 

dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ // Wait for x seconds to hide 
    [UIView animateWithDuration:fadeInterval animations:^{ 
     view.alpha = 0.0; // HIDE 
    } completion:^(BOOL finished) { 
     dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ // Wait for x seconds to show 
      [UIView animateWithDuration:fadeInterval animations:^{ 
       view.alpha = 1.0; // SHOW 
      }]; 
     }); 
    }]; 
}); 

dispatch_after(3)+animateWithDuration:animations:+animateWithDuration:animations:completion:


UPDATE

確定。根據你的評論我會讓事情變得更簡單。

第1步:使視圖消失。這可以通過將alpha(透明度)設置爲0,通過將隱藏設置爲YES或從其超級視圖中刪除視圖來完成。爲了一個簡單的效果,這次我將隱藏設置爲YES。

view.hidden = YES; 

第2步:使視圖在設定的時間量後消失。有很多方法可以做到這一點。我會堅持dispatch_after(),但我會讓它更容易理解。這一步有兩個部分。 a)部分設置了您希望視圖隱藏的時間。 b)部分隱藏了視圖。

// Part a) Set the time you want the view to disappear. 
double howLongBeforeDisappearing = 2.0; // seconds 
dispatch_time_t timeToDisappear = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(howLongBeforeDisappearing * NSEC_PER_SEC)); 

// Part b) Hide the view 
dispatch_after(timeToDisappear, dispatch_get_main_queue(), ^{ 
    view.hidden = YES; 
}); 

有很多的繞重要的代碼的東西,而是着眼於howLongBeforeDisappearing = 2.0view.hidden = YES。這說2秒後將view.hidden設置爲YES。

最後,我們需要反轉這個以使事情重新出現。爲此,除了這次我們將view.hidden設置爲NO之外,我們完全一樣。請記住,在設置時間重新出現時,我們需要添加等待視圖消失的時間。

// Part c) Set the time you want the view to reappear. 
double howLongBeforeReappearing = howLongBeforeDisappearing + 2.0; // seconds 
dispatch_time_t timeToReappear = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(howLongBeforeReappearing * NSEC_PER_SEC)); 

// Part d) Show the view 
dispatch_after(timeToReappear, dispatch_get_main_queue(), ^{ 
    view.hidden = NO; 
}); 

加上這一切,我們有最後一塊代碼。

// Part a) Set the time you want the view to disappear. 
double howLongBeforeDisappearing = 2.0; // seconds 
dispatch_time_t timeToDisappear = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(howLongBeforeDisappearing * NSEC_PER_SEC)); 

// Part b) Hide the view 
dispatch_after(timeToDisappear, dispatch_get_main_queue(), ^{ 
    view.hidden = YES; 
}); 

// Part c) Set the time you want the view to reappear. 
double howLongBeforeReappearing = howLongBeforeDisappearing + 2.0; // seconds 
dispatch_time_t timeToReappear = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(howLongBeforeReappearing * NSEC_PER_SEC)); 

// Part d) Show the view 
dispatch_after(timeToReappear, dispatch_get_main_queue(), ^{ 
    view.hidden = NO; 
}); 
+0

即時通訊新目標,並不真正瞭解,正在思考一些更簡單的事情,即生病也能理解。我不只是想把我不明白的算法。我感謝你的迴應,也許當我獲得更多經驗時,我會回來檢查一下。 – 2013-02-08 19:21:13

+0

@ Thankmelater23我更新了示例,讓我知道這是否有幫助。 – 2013-02-09 14:48:19

0

這是如何進行的按鈕閃爍

- (無效)blinkAll { [的NSTimer scheduledTimerWithTimeInterval:0.5目標:自選擇器:@selector(hideBoxes)USERINFO:無重複:NO] ;

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showBoxesCurrent) userInfo:nil repeats:NO]; 

[NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(hideBoxes) userInfo:nil repeats:NO]; 

[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showBoxesCurrent) userInfo:nil repeats:NO]; 

[NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(hideBoxes) userInfo:nil repeats:NO]; 

[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(showBoxesCurrent) userInfo:nil repeats:NO]; 

[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(hideBoxes) userInfo:nil repeats:NO]; 

[NSTimer scheduledTimerWithTimeInterval:3.5 target:self selector:@selector(enableGamePlay) userInfo:nil repeats:NO]; 

}

的showBoxesCurrent和hideBoxes功能是基本的循環,即得到按鈕所有陣列,並將它們設置到由功能的NSTimer激活隱藏或不隱藏這就是。