我試圖創建一個記憶遊戲,但在某個時間點和時間,我想要一個UIbutton上的圖像被閃現。對於x秒的秒數,我希望它們是可見的,對於x秒的時間我希望它們被隱藏。我被卡住了,只是想讓別人給我一個可行的算法。謝謝。在UIButton上創建閃爍的圖像?
回答
基本方法是通過設置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.0
和view.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;
});
即時通訊新目標,並不真正瞭解,正在思考一些更簡單的事情,即生病也能理解。我不只是想把我不明白的算法。我感謝你的迴應,也許當我獲得更多經驗時,我會回來檢查一下。 – 2013-02-08 19:21:13
@ Thankmelater23我更新了示例,讓我知道這是否有幫助。 – 2013-02-09 14:48:19
這是如何進行的按鈕閃爍
- (無效)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激活隱藏或不隱藏這就是。
- 1. 的UIButton像閃爍
- 2. iOS上的UIButton圖像內容模式在TouchUpInside上閃爍
- 3. 圖像不閃爍
- 4. DataGridViewImageColumn,圖像閃爍
- 5. 上傳圖像閃爍在c + +
- 6. 在pygame中閃爍圖像
- 7. QML閃爍與圖像上的動畫
- 8. UIButton按下時閃爍
- 9. Tkinter - 在畫布上創建閃爍的圖形?
- 10. 爲什麼圖像閃爍?
- 11. Opera 11.5圖像閃爍
- 12. android andengine PNG圖像閃爍
- 13. 爪哇 - 圖像閃爍
- 14. Nivo滑塊圖像閃爍
- 15. 在JPanel中閃爍的圖像
- 16. 在android中閃爍的圖像
- 17. 粘滯導航像閃爍燈閃爍
- 18. 如何在android中創建閃爍線?
- 19. 如何在matlab中閃爍圖像gui
- 20. 圖像在WP7中閃爍ListBox
- 21. Android:創建點擊時閃爍的TextView
- 22. 在CSS -webkit-transform的結尾處:翻譯圖像閃爍或閃爍
- 23. 如何在Corona SDK中的圖像上顯示閃爍文字?
- 24. 已調整大小的圖像在Firefox上閃爍
- 25. React Native - 爲什麼我的圖像按鈕在Android上閃爍?
- 26. IE8中的一些圖像閃爍
- 27. wxPython的 - 調整圖像無閃爍?
- 28. Adobe Flex中的圖像閃爍
- 29. jquery閃爍的背景圖像
- 30. Android圖像在recyclerview中的圖像閃爍
請參閱http:// stackoverflow。com/questions/2823635 /我怎麼能使一個uibutton閃光與輝光或變化它的形象爲spli – 2013-02-08 14:23:19