iOS开发-舒尔特表
周末閑來無事,看一個(gè)概念,挺有意思的,舒爾特表,網(wǎng)上也有很多人寫過類似的Demo,本人閑來無事也寫了一下,舒爾特表聽起來很高大上的樣子,不過本人的理解就是一個(gè)正方形的矩陣中放的各種小格子,可以是字母,數(shù)字或者說是文字,舒爾特表可以通過動(dòng)態(tài)的練習(xí)鍛煉視神經(jīng)末梢。心理學(xué)上用此表來研究和發(fā)展心理感知的速度,其中包括視覺定向搜索運(yùn)動(dòng)的速度。培養(yǎng)注意力集中、分配、控制能力;拓展視幅;加快視頻;提高視覺的穩(wěn)定性、辨別力、定向搜索能力。練習(xí)的時(shí)間越長(zhǎng),看表所需的時(shí)間會(huì)越短。隨著練習(xí)的深入,眼球的末梢視覺能力提高,不僅初學(xué)者可以有效地拓展視幅,加快閱讀節(jié)奏,鍛煉眼睛快速認(rèn)讀;而且對(duì)于進(jìn)入提高階段之后,同時(shí)拓展縱橫視幅,達(dá)到一目十行、一目一頁非常有效。每表按字符順序,迅速找全所有的字符,平均1個(gè)字符用1秒鐘成績(jī)?yōu)閮?yōu)良,即9格用9秒、16格用16秒、25格用25秒。(百度百科)
頁面布局
根據(jù)上面的概念,大概頁面布局就是3*3的九宮格,一般是選擇數(shù)字練習(xí),也沒有特別的多弄弄,Main.storyBoard的布局如下:
需要將所有的按鈕弄成一個(gè)集合,還有就是所有按鈕共用一個(gè)事件:
集合演示:
按鈕共用事件演示:
Demo實(shí)現(xiàn)
上面中的界面主要都是數(shù)字1,因此需要的一個(gè)方法生成一個(gè)隨機(jī)的數(shù)組,方法如下,生成一個(gè)隨機(jī)不重復(fù)的數(shù)組大概的規(guī)則就是首先頂一個(gè)數(shù)組,之后的話,需要將數(shù)組打亂,使用隨機(jī)數(shù)隨機(jī)生成索引即可:
- (NSArray *)getDataArray{
//需要展示的數(shù)組
NSMutableArray *arr=[NSMutableArray array];
for (NSInteger i=1; i<10; i++) {
[arr addObject:@(i)];
}
NSInteger count=[arr count];
//生成隨機(jī)數(shù)組
for (NSInteger i=0; i<count; i++) {
NSInteger index=arc4random()%(count-i)+i;
[arr exchangeObjectAtIndex:index withObjectAtIndex:i];
}
return arr;
}
將數(shù)組中的值賦值給頁面的按鈕:
- (void)initButtonTitle{
//實(shí)例化結(jié)果集的可變數(shù)組
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];
//重新開始的時(shí)候要重新設(shè)置按鈕的背景和狀態(tài)
[button setBackgroundColor:[UIColor yellowColor]];
[button setEnabled:YES];
}
[_timerLabel setText:@"00:00"];
self.timer=[NSTimer scheduledTimerWithTimeInterval:0.1 invocation:nil repeats:YES];
}
在viewDidLoad啟動(dòng)的時(shí)候調(diào)用對(duì)應(yīng)的程序:
[self initButtonTitle];
這個(gè)時(shí)候看到頁面的布局應(yīng)該是這樣的:
做到這一步基本上這個(gè)程序完成差不多了,然后就是計(jì)時(shí),完成之后統(tǒng)計(jì),閑來看下具體的效果,然后看代碼可能會(huì)更好一點(diǎn):
功能基本上就是設(shè)置按鈕的背景顏色,是否可以點(diǎn)擊,計(jì)時(shí),清零,彈框,顯示之前點(diǎn)擊的結(jié)果,一步一步的分析:
定義成員變量計(jì)時(shí)和存儲(chǔ)結(jié)果:
@interface ViewController () @property NSMutableArray *resultArr; @property NSTimer *timer; @property NSDate *beginDate; @end
設(shè)置定時(shí)器:
self.timer=[NSTimer scheduledTimerWithTimeInterval:0.1 invocation:nil repeats:YES];
設(shè)置按鈕的背景顏色和顯隱:
[button setBackgroundColor:[UIColor yellowColor]];
[button setEnabled:YES];
統(tǒng)計(jì)時(shí)間:
NSInteger allTime=[_timer.fireDate timeIntervalSinceDate:_beginDate];
NSString *timeFormat=[NSString stringWithFormat:@"%02ld:%02ld",allTime/1000,allTime%1000];
[_timerLabel setText:timeFormat];
判斷結(jié)果,如果所有的結(jié)果是升序的那么是成功的,否則就是失敗的:
//判斷一個(gè)數(shù)組是不是升序
- (BOOL)getArrayAsc:(NSArray *)originalArr{
for (NSInteger i=0; i<[originalArr count]-1; i++) {
if (originalArr[i]>originalArr[i+1]) {
return NO;
}
}
return YES;
}
所有按鈕的點(diǎn)擊事件如下:
- (IBAction)getResult:(id)sender {
UIButton *button=(UIButton *)sender;
//設(shè)置背景顏色
[button setBackgroundColor:[UIColor greenColor]];
//按鈕點(diǎn)擊一次就不再點(diǎn)擊
[button setEnabled:NO];
NSInteger value=[[[button titleLabel] text] integerValue];
[self.resultArr addObject:[NSNumber numberWithInteger:value]];
//點(diǎn)擊第一個(gè)按鈕之后設(shè)置開始時(shí)間
if ([self.resultArr count]==1) {
//游戲開始時(shí)間
_beginDate=[NSDate date];
//游戲開始
[_timer fire];
}
NSInteger allTime=[_timer.fireDate timeIntervalSinceDate:_beginDate];
NSString *timeFormat=[NSString stringWithFormat:@"%02ld:%02ld",allTime/1000,allTime%1000];
[_timerLabel setText:timeFormat];
//所有的點(diǎn)擊完成之后的調(diào)用
if ([self.resultArr count]==9) {
//銷毀定時(shí)器
[_timer invalidate];
//彈框
NSString *tip;
if ([self getArrayAsc:self.resultArr]) {
tip=@"恭喜你,通過比賽";
}else{
tip=@"不好意思,比賽失敗";
}
//將點(diǎn)擊的結(jié)果使用逗號(hào)進(jìn)行拼接
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{
//實(shí)例化結(jié)果集的可變數(shù)組
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];
//重新開始的時(shí)候要重新設(shè)置按鈕的背景和狀態(tài)
[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{
//需要展示的數(shù)組
NSMutableArray *arr=[NSMutableArray array];
for (NSInteger i=1; i<10; i++) {
[arr addObject:@(i)];
}
NSInteger count=[arr count];
//生成隨機(jī)數(shù)組
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;
//設(shè)置背景顏色
[button setBackgroundColor:[UIColor greenColor]];
//按鈕點(diǎn)擊一次就不再點(diǎn)擊
[button setEnabled:NO];
NSInteger value=[[[button titleLabel] text] integerValue];
[self.resultArr addObject:[NSNumber numberWithInteger:value]];
//點(diǎn)擊第一個(gè)按鈕之后設(shè)置開始時(shí)間
if ([self.resultArr count]==1) {
//游戲開始時(shí)間
_beginDate=[NSDate date];
//游戲開始
[_timer fire];
}
NSInteger allTime=[_timer.fireDate timeIntervalSinceDate:_beginDate];
NSString *timeFormat=[NSString stringWithFormat:@"%02ld:%02ld",allTime/1000,allTime%1000];
[_timerLabel setText:timeFormat];
//所有的點(diǎn)擊完成之后的調(diào)用
if ([self.resultArr count]==9) {
//銷毀定時(shí)器
[_timer invalidate];
//彈框
NSString *tip;
if ([self getArrayAsc:self.resultArr]) {
tip=@"恭喜你,通過比賽";
}else{
tip=@"不好意思,比賽失敗";
}
//將點(diǎn)擊的結(jié)果使用逗號(hào)進(jìn)行拼接
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.
}
//判斷一個(gè)數(shù)組是不是升序
- (BOOL)getArrayAsc:(NSArray *)originalArr{
for (NSInteger i=0; i<[originalArr count]-1; i++) {
if (originalArr[i]>originalArr[i+1]) {
return NO;
}
}
return YES;
}
@end
總結(jié)
以上是生活随笔為你收集整理的iOS开发-舒尔特表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: modis数据处理的坑(MOD02,mo
- 下一篇: n阶行列式计算