我正在尝试创建一个记忆游戏,但在某些时候我希望 UIbutton 上的图像被闪烁。对于 x 秒,我希望它们可见,对于 x 秒,我希望它们被隐藏。我被困住了,只想有人给我一个可行的算法。谢谢。
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:
。
更新
好的。根据您的评论,我会让事情变得更简单。
第 1 步:使视图消失。这可以通过将 alpha(透明度)设置为 0、将 hidden 设置为 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;
});
这就是我让按钮闪烁的方式
-(void) blinkAll { [NSTimer scheduledTimerWithTimeInterval:.5 目标:self 选择器:@selector(hideBoxes) userInfo:nil repeats: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 函数激活。