页面布局
根据上面的概念,大概页面布局就是3*3的九宫格,一般是选择数字练习,也没有特别的多弄弄,
Demo实现
上面中的界面主要都是数字1,因此需要的一个方法生成一个随机的数组,方法如下,生成一个随机不重复的数组大概的规则就是首先顶一个数组,之后的话,需要将数组打乱,使用随机数随机生成索引即可:
- (NSArray *)getDataArray{
//需要展示的数组
NSMutableArray *arr=[NSMutableArray array];
for (NSInteger i=1; i<10; i++) {
[arr addObject:@(i)];
}
NSInteger count=[arr count];
//生成随机数组
for (NSInteger i=0; i NSInteger index=arc4random()%(count-i)+i; [arr exchangeObjectAtIndex:index withObjectAtIndex:i]; } return arr; } 将数组中的值赋值给页面的按钮: - (void)initButtonTitle{ //实例化结果集的可变数组 self.resultArr=[NSMutableArray array]; NSArray *arr=[self getDataArray]; for (UIButton *button in self.visionButtons) { NSString *result=[arr[button.tag-1]stringValue]; [button setTitle:result forState:UIControlStateNormal]; //重新开始的时候要重新设置按钮的背景和状态 [button setBackgroundColor:[UIColor yellowColor]]; [button setEnabled:YES]; } [_timerLabel setText:@"00:00"]; self.timer=[NSTimer scheduledTimerWithTimeInterval:0.1 invocation:nil repeats:YES]; } 在viewDidLoad启动的时候调用对应的程序: 1 [self initButtonTitle]; 做到这一步基本上这个程序完成差不多了,然后就是计时,完成之后统计,闲来看下具体的效果,然后看代码可能会更好一点: 功能基本上就是设置按钮的背景颜色,是否可以点击,计时,清零,弹框,显示之前点击的结果,一步一步的分析: 定义成员变量计时和存储结果: @interface ViewController () @property NSMutableArray *resultArr; @property NSTimer *timer; @property NSDate *beginDate; @end 设置定时器: self.timer=[NSTimer scheduledTimerWithTimeInterval:0.1 invocation:nil repeats:YES]; 设置按钮的背景颜色和显隐: [button setBackgroundColor:[UIColor yellowColor]]; [button setEnabled:YES]; 统计时间: NSInteger allTime=[_timer.fireDate timeIntervalSinceDate:_beginDate]; NSString *timeFormat=[NSString stringWithFormat:@"%02ld:%02ld",allTime/1000,allTime%1000]; [_timerLabel setText:timeFormat]; 判断结果,如果所有的结果是升序的那么是成功的,否则就是失败的: //判断一个数组是不是升序 - (BOOL)getArrayAsc:(NSArray *)originalArr{ for (NSInteger i=0; i<[originalArr count]-1; i++) { if (originalArr[i]>originalArr[i+1]) { return NO; } } return YES; } 所有按钮的点击事件如下: - (IBAction)getResult:(id)sender { UIButton *button=(UIButton *)sender; //设置背景颜色 [button setBackgroundColor:[UIColor greenColor]]; //按钮点击一次就不再点击 [button setEnabled:NO]; NSInteger value=[[[button titleLabel] text] integerValue]; [self.resultArr addObject:[NSNumber numberWithInteger:value]]; //点击第一个按钮之后设置开始时间 if ([self.resultArr count]==1) { //游戏开始时间 _beginDate=[NSDate date]; //游戏开始 [_timer fire]; } NSInteger allTime=[_timer.fireDate timeIntervalSinceDate:_beginDate]; NSString *timeFormat=[NSString stringWithFormat:@"%02ld:%02ld",allTime/1000,allTime%1000]; [_timerLabel setText:timeFormat]; //所有的点击完成之后的调用 if ([self.resultArr count]==9) { //销毁定时器 [_timer invalidate]; //弹框 NSString *tip; if ([self getArrayAsc:self.resultArr]) { tip=@"恭喜你,通过比赛"; }else{ tip=@"不好意思,比赛失败"; } //将点击的结果使用逗号进行拼接 NSString *resultStr=[NSString stringWithFormat:@"%@",[self.resultArr componentsJoinedByString:@","]]; UIAlertView *alterView=[[UIAlertView alloc] initWithTitle:tip message:resultStr delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alterView show]; } } ViewController.m中的代码: // // ViewController.m // TableVision // // Created by keso on 15/1/18. // Copyright (c) 2015年 keso. All rights reserved. // #import "ViewController.h" @interface ViewController () @property NSMutableArray *resultArr; @property NSTimer *timer; @property NSDate *beginDate; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self initButtonTitle]; // self.timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:nil userInfo:nil repeats:YES]; } - (void)triggerTime:(NSTimer *)sender{ } - (void)initButtonTitle{ //实例化结果集的可变数组 self.resultArr=[NSMutableArray array]; NSArray *arr=[self getDataArray]; for (UIButton *button in self.visionButtons) { NSString *result=[arr[button.tag-1]stringValue]; [button setTitle:result forState:UIControlStateNormal]; //重新开始的时候要重新设置按钮的背景和状态 [button setBackgroundColor:[UIColor yellowColor]]; [button setEnabled:YES]; } [_timerLabel setText:@"00:00"]; self.timer=[NSTimer scheduledTimerWithTimeInterval:0.1 invocation:nil repeats:YES]; } - (IBAction)oneMore:(id)sender { [self initButtonTitle]; } - (NSArray *)getDataArray{ //需要展示的数组 NSMutableArray *arr=[NSMutableArray array]; for (NSInteger i=1; i<10; i++) { [arr addObject:@(i)]; } NSInteger count=[arr count]; //生成随机数组 for (NSInteger i=0; i NSInteger index=arc4random()%(count-i)+i; [arr exchangeObjectAtIndex:index withObjectAtIndex:i]; } return arr; } - (IBAction)getResult:(id)sender { UIButton *button=(UIButton *)sender; //设置背景颜色 [button setBackgroundColor:[UIColor greenColor]]; //按钮点击一次就不再点击 [button setEnabled:NO]; NSInteger value=[[[button titleLabel] text] integerValue]; [self.resultArr addObject:[NSNumber numberWithInteger:value]]; //点击第一个按钮之后设置开始时间 if ([self.resultArr count]==1) { //游戏开始时间 _beginDate=[NSDate date]; //游戏开始 [_timer fire]; } NSInteger allTime=[_timer.fireDate timeIntervalSinceDate:_beginDate]; NSString *timeFormat=[NSString stringWithFormat:@"%02ld:%02ld",allTime/1000,allTime%1000]; [_timerLabel setText:timeFormat]; //所有的点击完成之后的调用 if ([self.resultArr count]==9) { //销毁定时器 [_timer invalidate]; //弹框 NSString *tip; if ([self getArrayAsc:self.resultArr]) { tip=@"恭喜你,通过比赛"; }else{ tip=@"不好意思,比赛失败"; } //将点击的结果使用逗号进行拼接 NSString *resultStr=[NSString stringWithFormat:@"%@",[self.resultArr componentsJoinedByString:@","]]; UIAlertView *alterView=[[UIAlertView alloc] initWithTitle:tip message:resultStr delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alterView show]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //判断一个数组是不是升序 - (BOOL)getArrayAsc:(NSArray *)originalArr{ for (NSInteger i=0; i<[originalArr count]-1; i++) { if (originalArr[i]>originalArr[i+1]) { return NO; } } return YES; } @end www.2cto.com 说明:博客经个人辛苦努力所得,如有转载会特别申明,博客不求技惊四座,但求与有缘人分享个人学习知识,生活学习提高之用,博客所有权归本人和博客园所有,如有转载请在显著位置给出博文链接和作者姓名,否则本人将付诸法律,iOS开发舒尔特表
,电脑资料
《iOS开发舒尔特表》(https://www.unjs.com)。