2013-02-08 145 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激活隐藏或不隐藏这就是。