標籤:
周末閑來無事,看一個概念,挺有意思的,舒爾特表,網上也有很多人寫過類似的Demo,本人閑來無事也寫了一下,舒爾特表聽起來很高大上的樣子,不過本人的理解就是一個正方形的矩陣中放的各種小格子,可以是字母,數字或者說是文字,舒爾特表可以通過動態練習鍛煉視神經末梢。心理學上用此表來研究和發展心理感知的速度,其中包括視覺定向搜尋運動的速度。培養注意力集中、分配、控制能力;拓展視幅;加快視頻;提高視覺的穩定性、辨別力、定向搜尋能力。練習的時間越長,看錶所需的時間會越短。隨著練習的深入,眼球的末梢視覺能力提高,不僅初學者可以有效地拓展視幅,加快閱讀節奏,鍛煉眼睛快速認讀;而且對於進入提高階段之後,同時拓展縱橫視幅,達到一目十行、一目一頁非常有效。每表按字元順序,迅速找全所有的字元,平均1個字元用1秒鐘成績為優良,即9格用9秒、16格用16秒、25格用25秒。(百度百科)
頁面配置
根據上面的概念,大概頁面配置就是3*3的九宮格,一般是選擇數字練習,也沒有特別的多弄弄,Main.storyBoard的布局如下:
需要將所有的按鈕弄成一個集合,還有就是所有按鈕共用一個事件:
集合示範:
按鈕共用事件示範:
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<count; 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啟動的時候調用對應的程式:
[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]) { [email protected]"恭喜你,通過比賽"; }else{ [email protected]"不好意思,比賽失敗"; } //將點擊的結果使用逗號進行拼接 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<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]) { [email protected]"恭喜你,通過比賽"; }else{ [email protected]"不好意思,比賽失敗"; } //將點擊的結果使用逗號進行拼接 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
iOS開發-舒爾特表