【问题标题】:How can I show a sequence of buttons highlighting?如何显示一系列突出显示的按钮?
【发布时间】:2010-12-12 22:11:54
【问题描述】:

类似于以前的西蒙游戏,我想向用户展示一个按钮序列,然后让他们重复它。我卡住的地方是显示第一个按钮突出显示 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 将突出显示,但不会发生任何后续操作。

【问题讨论】:

    标签: iphone cocoa-touch uikit core-animation


    【解决方案1】:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      相关资源
      最近更新 更多