24点小游戏ios开发(一)

cocoa社区有很多iOS的小游戏源码(没有24点的大概是因为它落后了...?),我下载了很多来作为参考。游戏源码已传至github:https://github.com/DF-L/24

先是最终的界面:

24点小游戏ios开发(一)_第1张图片

先是一个计时器的代码,参考了一个touchme的源码。实现按下开始按钮之后开始计时的功能,并记录每次的计时到总时间

.h文件中:
NSTimer *timer;
-(void)subtracTime;
.m文件中:
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                             target:self
                                           selector:@selector(subtracTime)
                                           userInfo:nil
                                            repeats:YES];//计时器
-(void)subtracTime
{//计时器
    if(count!=0){
        second+=1;
        lable18++;
    }
    timerLabel.text = [NSString stringWithFormat:@"Time:%d",second];
    if(second == 30)
    {
        [timer invalidate];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Times Out"
                                                                       message:[NSString stringWithFormat:@"Score:%d",score] preferredStyle:UIAlertControllerStyleAlert];
        
        scoreLabel.text = [NSString stringWithFormat:@"Score:%d",score];
        
        UIAlertAction *againAction = [UIAlertAction actionWithTitle:@"Again"
                                                              style:UIAlertActionStyleCancel
                                                            handler:nil];
        [alert addAction:againAction];
        [self presentViewController:alert animated:YES completion:nil];
        [self setGame];
    }
}

timerLabel.text = [NSString stringWithFormat:@"倒计时:%d",second];
lable10.text =[NSString stringWithFormat:@"总时间:%d",lable18];


你可能感兴趣的:(ios开发)