網頁

2012年6月28日 星期四

Countdown Timer in Xcode

How to create a countdown timer in iPhone app.

.h
 
@interface ViewController : UIViewController
{
    IBOutlet UILabel  *countDownLabel;

    NSTimer *gameTimeCount_Timer;
    
    int gameCountDown_int;    
    int gameCountDown_minutes, gameCountDown_seconds;

    
}
@property (nonatomic, retain) UILabel *countDownLabel;
.m

 
#import "ViewController.h"

#define GAME_COUNTDOWN    71


@interface ViewController ()

@end

@implementation ViewController
@synthesize countDownLabel;


- (void)viewWillAppear:(BOOL)animated {
    
    gameCountDown_int = GAME_COUNTDOWN;
    
    gameTimeCount_Timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(GameTimer_Count) userInfo:nil repeats:YES];
    
}

- (void)GameTimer_Count {
    

    if (gameCountDown_int > 0)
    {
        gameCountDown_int-- ;
        
        gameCountDown_minutes = (gameCountDown_int % 3600) / 60;
        gameCountDown_seconds = (gameCountDown_int % 3600) % 60;
        
        countDownLabel.text =   [NSString stringWithFormat:@"%02d:%02d", gameCountDown_minutes, gameCountDown_seconds]; 
    }
    
}

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。