zoukankan      html  css  js  c++  java
  • iOS开发-舒尔特表

    周末闲来无事,看一个概念,挺有意思的,舒尔特表,网上也有很多人写过类似的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]) {
                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<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
  • 相关阅读:
    远程连接redis服务
    redis的安装以及启动
    Easyui学习之右键菜单easyui-menu
    富文本编辑器KindEditor的使用
    zookeeper启动失败解决方法
    在TortoiseSVN使用clean up
    kettle性能优化
    idea快捷键
    Spring Cloud服务网关 Zuul Filter使用
    添加路由
  • 原文地址:https://www.cnblogs.com/xiaofeixiang/p/4232605.html
Copyright © 2011-2022 走看看