与旧版的Simon游戏类似,我想向用户显示一个按钮序列&然后让他们重复一遍。我被困住的地方显示第一个按钮突出显示为500毫秒,等待100毫秒,显示第二个突出显示500毫秒,等待另一个100毫秒,显示第三个&等等。
从其他Stackoverflower'ers我已经到了这个街区:
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会突出显示,但后续动作都不会发生。
答案 0 :(得分:4)
使用Core Animation可能有办法做到这一点,但没有必要。您没有为突出显示的属性设置动画,只是打开和关闭它。
我创建了一个基于视图的简单iPhone应用程序来与计时器一起执行此操作。以下是视图控制器中的代码:
<强> 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;
}