这一章主要介绍了Alert View控件,userInfo:nil代表用户信息不存在
保存时间值NSDate,通过NSDate对象获取时间差
//创建一个整数数据类型的数字,数据为0不显示扑克牌的黑桃
int spadeOn = 0;
- (void)viewDidLoad {
[super viewDidLoad];
//建立一个提示框对象alert,定义提示框的内容
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"第12天 考反映扑克游戏"
message:@"当黑桃现时以最快的速度按下扑克"
delegate:self cancelButtonTitle:@"游戏开始"
otherButtonTitles: nil];
[alert show];
}
//单击按钮关闭提示框
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
pokerImage.image = [UIImage imageNamed:@"pokerBack.png"];
//出现黑桃整数变量数据0,表示游戏中绿灯还没亮
spadeOn = 0;
[NSTimer scheduledTimerWithTimeInterval:(1.0)
target:self
selector:@selector(onOtherAceTimer)
userInfo:nil
repeats:NO];
}
//游戏中出现黑桃的时间差方法
- (void)onSpadesAceTimer
{
pokerImage.image = [UIImage imageNamed:@"SpadesAce.png"];
//黑桃出现
spadeOn = 1;
//获取时间差
self.startDate = [NSDate date];
}
//游戏中出现其他扑克的时间差方法
- (void)onOtherAceTimer
{
int rNumber = rand() % 3;
switch (rNumber) {
case 0:
pokerImage.image = [UIImage imageNamed:@"clubsAce.png"];
break;
case 1:
pokerImage.image = [UIImage imageNamed:@"diamonsAce.png"];
break;
case 2:
pokerImage.image = [UIImage imageNamed:@"heartsAce.png"];
break;
default:
break;
}
//建立游戏中的考反应的随机时间差数值
int delay = ((int) (random() % 7) + 1);
[NSTimer scheduledTimerWithTimeInterval:(3.0 + delay)
target:self
selector:@selector(onSpadesAceTimer)
userInfo:nil
repeats:NO];
}
//建立操作效应的方法,单击扑克的透明按钮计算反应时间,* -1000把数值转换为毫秒显示
- (IBAction)pokerPressed{
//定义程序中的反应时间数值,获取对象时间差数值self.startDate
double noSeconds = (double) [self.startDate timeIntervalSinceNow] * -1000;
//创建游戏的反应时间文字的变量对象,定以文字内容,返回反应时间
NSString *reactionTime= [[NSString alloc] initWithFormat:@"好样的! 你的响应速度是 %1.0f 毫秒. 创造更好的成绩...", noSeconds];
if(spadeOn == 0)
//更新反应时间文字对象的内容
reactionTime = @"请不要急,等到黑桃出现才按脚扑克";
//创建一个提示框对象
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"再来一次" message:reactionTime
delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
}