iOS开发-舒尔特表
周末闲来无事,看一个概念,挺有意思的,舒尔特表,网上也有很多人写过类似的Demo,本人闲来无事也写了一下,舒尔特表听起来很高大上的样子,不过本人的理解就是一个正方形的矩阵中放的各种小格子,可以是字母,数字或者说是文字,舒尔特表可以通过动态的练习锻炼视神经末梢。心理学上用此表来研究和发展心理感知的速度,其中包括视觉定向搜索运动的速度。培养注意力集中、分配、控制能力;拓展视幅;加快视频;提高视觉的稳定性、辨别力、定向搜索能力。练习的时间越长,看表所需的时间会越短。随着练习的深入,眼球的末梢视觉能力提高,不仅初学者可以有效地拓展视幅,加快阅读节奏,锻炼眼睛快速认读;而且对于进入提高阶段之后,同时拓展纵横视幅,达到一目十行、一目一页非常有效。每表按字符顺序,迅速找全所有的字符,平均1个字符用1秒钟成绩为优良,即9格用9秒、16格用16秒、25格用25秒。(百度百科)
页面布局
根据上面的概念,大概页面布局就是3*3的九宫格,一般是选择数字练习,也没有特别的多弄弄,Main.storyBoard的布局如下:
需要将所有的按钮弄成一个集合,还有就是所有按钮共用一个事件:
集合演示:
按钮共用事件演示:
Demo实现
上面中的界面主要都是数字1,因此需要的一个方法生成一个随机的数组,方法如下,生成一个随机不重复的数组大概的规则就是首先顶一个数组,之后的话,需要将数组打乱,使用随机数随机生成索引即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | - ( 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<count; i++) { NSInteger index=arc4random()%(count-i)+i; [arr exchangeObjectAtIndex:index withObjectAtIndex:i]; } return arr; } |
将数组中的值赋值给页面的按钮:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | - ( 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]; |
这个时候看到页面的布局应该是这样的:
做到这一步基本上这个程序完成差不多了,然后就是计时,完成之后统计,闲来看下具体的效果,然后看代码可能会更好一点:
功能基本上就是设置按钮的背景颜色,是否可以点击,计时,清零,弹框,显示之前点击的结果,一步一步的分析:
定义成员变量计时和存储结果:
1 2 3 4 5 | @interface ViewController () @property NSMutableArray *resultArr; @property NSTimer *timer; @property NSDate *beginDate; @end |
设置定时器:
1 | self .timer=[ NSTimer scheduledTimerWithTimeInterval:0.1 invocation: nil repeats: YES ]; |
设置按钮的背景颜色和显隐:
1 2 | [button setBackgroundColor:[UIColor yellowColor]]; [button setEnabled: YES ]; |
统计时间:
1 2 3 | NSInteger allTime=[_timer.fireDate timeIntervalSinceDate:_beginDate]; NSString *timeFormat=[ NSString stringWithFormat:@ "%02ld:%02ld" ,allTime/1000,allTime%1000]; [_timerLabel setText:timeFormat]; |
判断结果,如果所有的结果是升序的那么是成功的,否则就是失败的:
1 2 3 4 5 6 7 8 9 | //判断一个数组是不是升序 - ( BOOL )getArrayAsc:( NSArray *)originalArr{ for ( NSInteger i=0; i<[originalArr count]-1; i++) { if (originalArr[i]>originalArr[i+1]) { return NO ; } } return YES ; } |
所有按钮的点击事件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | - ( 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中的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | // // 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<count; 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 |
出处:http://www.cnblogs.com/xiaofeixiang
说明:博客经个人辛苦努力所得,如有转载会特别申明,博客不求技惊四座,但求与有缘人分享个人学习知识,生活学习提高之用,博客所有权归本人和博客园所有,如有转载请在显著位置给出博文链接和作者姓名,否则本人将付诸法律。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述