ページ

2011年5月27日金曜日

XcodeでシンプルなiPhone用StopWatchを作ってみた





stopWatchViewController.h
#import <UIKit/UIKit.h>

@interface stopWatchViewController : UIViewController {

 IBOutlet UILabel *time;
 
 NSTimer *timeTicker;
}

@property(nonatomic, retain) UILabel *time;

-(IBAction) strart:(id) sender;
-(IBAction) stop:(id) sender;
-(IBAction) clear:(id) sender;

-(void)showActivity;

@end

stopWatchViewController.m
#import "stopWatchViewController.h"

@implementation stopWatchViewController
@synthesize time;

-(IBAction) strart:(id) sender{
 timeTicker = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
}

-(IBAction) stop:(id) sender{
 [timeTicker invalidate];
}

-(IBAction) clear:(id) sender{
 time.text = @"00.00";
}

-(void)showActivity{
 float currentTime = [time.text floatValue];
 float displayTime = currentTime + 0.01;
 
 time.text = [NSString stringWithFormat:@"%.2f", displayTime];
}
- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
 
 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

参考


YouTube - iPhone SDK Tutorial: Timer/Stop Watch
http://www.youtube.com/watch?v=5jmTQi98vec&hd=1


0 件のコメント:

コメントを投稿