博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios 简单的计时器游戏 NSUserDefaults NSDate NSTimer
阅读量:7126 次
发布时间:2019-06-28

本文共 5307 字,大约阅读时间需要 17 分钟。

hot3.png

1.NSUserDefaults是应用程序实现过程中只会存在单独一个的对象,故可以用它记录小型的数据如系统偏好设置等

2.初始化:

    self.myuserDefaults = [NSUserDefaults standardUserDefaults];

3.取值:

    [_myuserDefaults objectForKey:KMaxScore];

4.赋值:

    [_myuserDefaults setObject:[NSNumber numberWithInt:0] forKey:KMaxScore];

5.NSDate是日期的数据类型

    self.startDate = [NSDate date];//记录当前时间

6.计算间隔时间

    self.timeUse = [_gameTimer.fireDate timeIntervalSinceDate:_startDate];

7.初始化计时器

    self.gameTimer = [NSTimer scheduledTimerWithTimeInterval:_timeBetween target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];

8.计时器响应时间

    self.timeUse = [_gameTimer.fireDate timeIntervalSinceDate:_startDate];

一个简单的计时器小游戏

#define KMaxScore @"KMaxScore"@interface ViewController ()
@property (nonatomic, strong) UILabel *timeLabel;@property (nonatomic, strong) UILabel *scoreLabel;@property (nonatomic, strong) UILabel *animationLabel;@property (nonatomic, strong) UIButton *aButton;@property (nonatomic, strong) UIAlertView *myAlertView;@property (nonatomic, strong) NSDate *startDate;@property (nonatomic, strong) NSTimer *gameTimer;@property (nonatomic, assign) NSInteger timeBetween;//button出现的时间间隔@property (nonatomic, assign) NSInteger buttonX;@property (nonatomic, assign) NSInteger buttonY;@property (nonatomic, assign) NSInteger score;@property (nonatomic, assign) NSInteger timeUse;@property (nonatomic, strong) NSUserDefaults *myuserDefaults;@end@implementation ViewController            - (void)viewDidLoad {    [super viewDidLoad];        [self initTheData];    [self initTheGame];}- (void)initTheData{    //判断是否第一次启动游戏    if ([_myuserDefaults objectForKey:KMaxScore] == nil) {        NSLog(@"%@", [_myuserDefaults objectForKey:KMaxScore]);        self.myuserDefaults = [NSUserDefaults standardUserDefaults];        [_myuserDefaults setObject:[NSNumber numberWithInt:0] forKey:KMaxScore];//        NSLog(@"haha");    }    NSLog(@"%@", [_myuserDefaults objectForKey:KMaxScore]);//0}- (void) initTheGame{    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];    [bgImageView setImage:[UIImage imageNamed:@"djfoj.jpg"]];    [self.view addSubview:bgImageView];//    NSLog(@"haha");//    self.timeUse = 0;        self.animationLabel = [[UILabel alloc] init];        self.timeBetween = 1;        self.startDate = [NSDate date];    self.gameTimer = [NSTimer scheduledTimerWithTimeInterval:_timeBetween target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];        self.score = 0;        self.scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, 320, 40)];    [_scoreLabel setText:[NSString stringWithFormat:@"得分:%d", _score]];    [_scoreLabel setTextAlignment:NSTextAlignmentCenter];    [self.view addSubview:_scoreLabel];        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 400, 320, 60)];    [_timeLabel setTextAlignment:NSTextAlignmentCenter];    [self.view addSubview:_timeLabel];        self.aButton = [UIButton buttonWithType:UIButtonTypeCustom];}- (void)timerFired:(NSTimer *) timer{    [_aButton setBackgroundImage:[UIImage imageNamed:@"Unlock_DotLock1_Selected"] forState:UIControlStateNormal];    self.buttonX = arc4random_uniform(220) + 50;    self.buttonY = arc4random_uniform(360) + 50;    [_aButton setFrame:CGRectMake(_buttonX, _buttonY, 52, 52)];    [_aButton addTarget:self action:@selector(buttonTouch:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:_aButton];    self.timeUse = [_gameTimer.fireDate timeIntervalSinceDate:_startDate];    [_timeLabel setText:[NSString stringWithFormat:@"剩余时间: %d", 61 - _timeUse]];        if (_timeUse >= 56) {        [_timeLabel setAlpha:1];        [_timeLabel setTextColor:[UIColor redColor]];        [_timeLabel setFont:[UIFont fontWithName:_timeLabel.font.fontName size:_timeLabel.font.pointSize + 3]];        [UIView animateWithDuration:0.8 animations:^{            [_timeLabel setAlpha:0];        }];    }        if (_timeUse == 61) {        if (_score > [[_myuserDefaults objectForKey:KMaxScore] intValue]) {            [_myuserDefaults setObject:[NSNumber numberWithInt:_score] forKey:KMaxScore];        }        self.myAlertView = [[UIAlertView alloc] initWithTitle:@"游戏结束" message:[NSString stringWithFormat:@"您的最高得分为:%d, 您本次的得分为:%d", [[_myuserDefaults objectForKey:KMaxScore] intValue], _score] delegate:self cancelButtonTitle:@"再玩一次" otherButtonTitles:@"退出游戏", nil];        [_myAlertView show];            [_gameTimer invalidate];    }}- (void)buttonTouch:(UIButton *)thisButton{    _score++;    [_scoreLabel setText:[NSString stringWithFormat:@"得分:%d", _score]];        [_animationLabel setAlpha:1];    [_animationLabel setFrame:CGRectMake(thisButton.frame.origin.x, thisButton.frame.origin.y, 40, 40)];    [_animationLabel setBackgroundColor:[UIColor greenColor]];    [_animationLabel setText:@"恭喜"];    [_animationLabel setTextAlignment:NSTextAlignmentCenter];    [_animationLabel setTextColor:[UIColor redColor]];    [self.view addSubview:_animationLabel];    [UIView animateWithDuration:1.0 animations:^{        [_animationLabel setAlpha:0];    }];}-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    if (buttonIndex == 0) {        [self initTheGame];    }else{        NSLog(@"已经退出游戏");    }}@end

转载于:https://my.oschina.net/u/1999967/blog/303588

你可能感兴趣的文章
一个例子探究jQuery的Ajax应用(二)
查看>>
PPT of "SharePoint 2007 网站性能优化"
查看>>
爪哇国新游记之三十四----Dom4j的XPath操作
查看>>
node17
查看>>
Java程序性能优化4
查看>>
第一次负责项目总结
查看>>
Azure Redis Cache (2) 创建和使用Azure Redis Cache
查看>>
python统计ES存储空间占用的代码
查看>>
成就连自己都惊讶的未来
查看>>
依赖倒置(DIP)与依赖注入(DI)
查看>>
mysql数据库授权
查看>>
Microstation
查看>>
深入浅出的英语口语700句zz
查看>>
linux编译安装php
查看>>
再谈奶牛问题
查看>>
第一个java程序------hello world!
查看>>
C#学习安排表
查看>>
在LINUX上创建GIT服务器【转】
查看>>
Linux内核跟踪之trace框架分析【转】
查看>>
XCode v9.6.2017.0830
查看>>