ios 简单的计时器游戏 NSUserDefaults NSDate NSTimer

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 ()<UIAlertViewDelegate>

@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

你可能感兴趣的:(ios 简单的计时器游戏 NSUserDefaults NSDate NSTimer)