如何显示一系列按钮突出显示? [英] How can I show a sequence of buttons highlighting?

查看:178
本文介绍了如何显示一系列按钮突出显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于老的Simon游戏,我想向用户显示按钮序列&然后让他们重复回来。我被卡住的地方显示第一个按钮突出显示,例如,500毫秒,等待100毫秒,显示第二个突出显示500毫秒,等待另一个100毫秒,显示第三&

Similar to the Simon games of old, I want to show the user a button sequence & then have them repeat it back. The place I'm stuck is showing the first button highlighted for, say, 500ms, waiting 100ms, showing the second highlighted for 500ms, waiting another 100ms, showing the 3rd & so on.

从其他Stackoverflower人我已经得到这个块:

From other Stackoverflower'ers I've gotten to this block:

redButton.highlighted = YES;

[UIView beginAnimations:@"" context:nil];
[UIView setAnimationStartDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[UIView setAnimationsEnabled:NO];
redButton.highlighted = NO;
[UIView commitAnimations];

[UIView beginAnimations:@"" context:nil];
[UIView setAnimationStartDate: [NSDate dateWithTimeIntervalSinceNow:2]];
[UIView setAnimationsEnabled:NO];
blueButton.highlighted = YES;
[UIView commitAnimations];

redButton会突出显示,但不会发生后续操作。

The redButton will highlight, but none of the subsequent actions take place.

推荐答案

Core Animation可能有一种方法,但这不是必须的。你不是动画高亮的属性,你只是打开和关闭它。

There might be a way to do this with Core Animation, but it is not necessary. You are not animating the highlighted property, you are just switching it on and off.

我创建了一个简单的基于视图的iPhone应用程序来做这个与计时器。以下是视图控制器的代码:

I created a simple view-based iPhone application to do this with timers. Here is the code from the view controller:

SimonTestViewController.h

#import <UIKit/UIKit.h>

@interface SimonTestViewController : UIViewController {
 IBOutlet UIButton *redButton;
 IBOutlet UIButton *blueButton;
 IBOutlet UIButton *greenButton;
 IBOutlet UIButton *yellowButton;
}

- (void)highlightButton:(UIButton*)button Delay:(double)delay;
- (void)highlightOn:(NSTimer*)timer;
- (void)highlightOff:(NSTimer*)timer;

@end

SimonTestViewController.m

#import "SimonTestViewController.h"

@implementation SimonTestViewController

const double HIGHLIGHT_SECONDS = 0.5; // 500 ms
const double NEXT_SECONDS = 0.6; // 600 ms

- (void)viewDidAppear:(BOOL)animated {
 [super viewDidAppear:animated];

 [self highlightButton:redButton Delay:0.0];
 [self highlightButton:blueButton Delay:NEXT_SECONDS];
 [self highlightButton:greenButton Delay:NEXT_SECONDS * 2];
 [self highlightButton:blueButton Delay:NEXT_SECONDS * 3];
 [self highlightButton:yellowButton Delay:NEXT_SECONDS * 4];
 [self highlightButton:redButton Delay:NEXT_SECONDS * 5];
}

- (void)highlightButton:(UIButton*)button Delay:(double)delay {
 [NSTimer scheduledTimerWithTimeInterval:delay target:self selector:@selector(highlightOn:) userInfo:button repeats:NO];
}

- (void)highlightOn:(NSTimer*)timer {
 UIButton *button = (UIButton*)[timer userInfo];

 button.highlighted = YES;

 [NSTimer scheduledTimerWithTimeInterval:HIGHLIGHT_SECONDS target:self selector:@selector(highlightOff:) userInfo:button repeats:NO];
}

- (void)highlightOff:(NSTimer*)timer {
 UIButton *button = (UIButton*)[timer userInfo];

 button.highlighted = NO;
}

这篇关于如何显示一系列按钮突出显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆