可能有一种方法可以通过核心动画实现这一点,但这不是必需的。您没有设置高亮显示属性的动画,您只是打开和关闭它。
我创建了一个简单的基于视图的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;
}