2009-11-04 3 views
0

Как и в старых играх 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 будет выделить, но ни один из последующих действий не происходит.

ответ

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; 
} 
Смежные вопросы