要求:用多中方法实现定时器
ViewController.h:
#import <UIKit/UIKit.h> #import "NSThread+test.h" @interface DXWViewController : UIViewController - (IBAction)clear:(id)sender; - (IBAction)click1:(id)sender; - (IBAction)click2:(id)sender; - (IBAction)click3:(id)sender; @property(retain,nonatomic) IBOutlet NSTimer *timer; @property (retain, nonatomic) IBOutlet UILabel *label; @property(retain,nonatomic) IBOutlet NSThread *thread; - (IBAction)click4:(id)sender; @end
#import "DXWViewController.h" #define START_BUTTON_Tag 1 #define STOP_BUTTON_Tag 2 @interface DXWViewController () @end @implementation DXWViewController - (void)viewDidLoad { [super viewDidLoad]; _thread = nil; //为什么不能初始化的时候给label赋值 //self.label = 0;//初始化 } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)dealloc { [_thread release]; [_timer release]; [_label release]; [super dealloc]; } //多线程调用 int i=0; BOOL flag = TRUE; -(void)countT { //i=0; //self.label.text = [NSString stringWithFormat:@"%i",i]; while(flag) { //测试 // i++; // NSLog(@"%d",i); // NSString *str = [NSString stringWithFormat:@"%i",i]; // NSLog(@"%@",str); // self.label.text = str; // [NSThread sleepForTimeInterval:1]; i++; NSString *str = [NSString stringWithFormat:@"%i",i]; NSLog(@"%@",str); self.label.text = str; // CGSize size = [self.label.text sizeWithFont:self.label.font]; // CGRect frame = CGRectMake(self.label.frame.origin.x,self.label.frame.origin.y, size.width,self.label.frame.size.height); // self.label.frame = frame; [NSThread sleepForTimeInterval:1];//休眠1秒 } } -(void)write { NSString *str = [NSString stringWithFormat:@"%i",i]; self.label.text = @"hi"; } //timer调用的函数 -(void)count:(NSTimer *)_timer { i++; NSString *str = [NSString stringWithFormat:@"%i",i]; self.label.text = str; CGSize size = [self.label.text sizeWithFont:self.label.font]; CGRect frame = CGRectMake(self.label.frame.origin.x,self.label.frame.origin.y, size.width, self.label.frame.size.height); NSLog(@"%i",i); self.label.frame = frame; } -(void)getSysDate:(NSTimer *)_timer { /* //倒计时 NSCalendar *calendar = [NSCalendar currentCalendar]; //设置目标时间 NSDateComponents *components = [[NSDateComponents alloc] init]; [components setYear:2012]; [components setMonth:8]; [components setDay:13]; [components setHour:12]; [components setMinute:0]; [components setSecond:0]; NSDate *fireDate = [calendar dateFromComponents:components];//目标时间 //当前时间 NSDate *today = [NSDate date]; unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *d = [calendar components:unitFlags fromDate:today toDate:fireDate options:0];//计算时间差 self.label.text = [NSString stringWithFormat:@"%d天%d小时%d%分%d秒", [d day], [d hour], [d minute], [d second]];//倒计时显示 */ NSDate *date=[NSDate date]; NSDateFormatter *formater=[[[NSDateFormatter alloc] init] autorelease]; //设置日期格式 formater.dateFormat=@"yyyy-MM-dd HH:mm:ss"; //HH大写代表24时制 hh代表12小时制 //把日期变成字符串 NSString *str=[formater stringFromDate:date]; //设置时区 formater.locale=[[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] autorelease]; //把字符串变成日期 //返回的是格林制时间 date=[formater dateFromString:@"2013-05-16 13:40:50"]; NSLog(@"字符串转化成日期是:%@",date); NSLog(@"%@",str); self.label.text = str; } - (IBAction)clear:(id)sender { self.label.text = @"0"; i = 0; } - (IBAction)click1:(id)sender { UIButton *button = (UIButton *)sender; // 如果是Start则执行start方法 //参数:最后一个参数如果是no则代表计时器执行一次 //_timer = nil; if (button.tag == START_BUTTON_Tag) { if ([_timer isValid]) { } else{ _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(count:) userInfo:nil repeats:YES]; } } //执行关闭方法 else if (button.tag == STOP_BUTTON_Tag) { if (_timer) { if ([_timer isValid]) { [_timer invalidate]; } } //必须要有这一步操作,不然会报错,指针不用的时候就让他置为空 _timer = nil; } } - (IBAction)click2:(id)sender { UIButton *button = (UIButton *)sender; if(button.tag == START_BUTTON_Tag) { //[NSThread sleepForTimeInterval:1]; // _thread = [[[NSThread alloc] initWithTarget:self selector:@selector(countT) object:nil] autorelease]; //获取一个单例 _thread = [NSThread getThread]; [_thread initWithTarget:self selector:@selector(countT) object:nil]; [_thread start]; flag = TRUE; } else if (button.tag == STOP_BUTTON_Tag) { if (![_thread isCancelled]) { [_thread cancel]; flag = FALSE; } //一旦不用了以后就要指向为空 _thread = nil; } } -(void)test { self.label.text = @"1"; } - (IBAction)click3:(id)sender { UIButton *button = (UIButton *)sender; if(button.tag == START_BUTTON_Tag) { if ([_timer isValid]) { } else{ _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(getSysDate:) userInfo:nil repeats:YES]; } //怎么调用外面的方法 } else if(button.tag == STOP_BUTTON_Tag) { if (_timer) { if ([_timer isValid]) { [_timer invalidate]; } } //必须要有这一步操作,不然会报错,指针不用的时候就让他置为空 _timer = nil; //self.label.text = @"0"; } } - (IBAction)click4:(id)sender { UIButton *button = (UIButton *)sender; if(button.tag == START_BUTTON_Tag) { self.label.text = @"hi"; } else if(button.tag == STOP_BUTTON_Tag) { self.label.text = @"hello"; } } @end
xib:
注意要设置成3.5英寸的
放一个label,几组button,分别一一对应,开始和结束
详细源码:http://download.csdn.net/detail/s10141303/5913117
==================== 迂者 丁小未 CSDN博客专栏=================
MyBlog:http://blog.csdn.net/dingxiaowei2013 MyQQ:1213250243
MyTel:13262983383
====================== 相互学习,共同进步 ===================