在UIButton上创建闪烁图像?

时间:2013-02-08 14:20:38

标签: objective-c ios6 xcode4.5 nstimer iphone-developer-program

我正在尝试创建一个记忆游戏但是在某些时候我想要在UI按钮上闪现图像。对于x秒的数量,我希望它们是可见的,并且对于x秒,我希望它们被隐藏。我被困住了,只想有人给我一个可行的算法。感谢。

2 个答案:

答案 0 :(得分: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.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;
});

答案 1 :(得分:0)

这就是我按钮闪烁的方式

- (void)blinkAll {     [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@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函数激活的按钮。