2013-8-9练习[多种方法制作一个计时器]

简介:

要求:用多中方法实现定时器

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

ViewController.m:

#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

====================== 相互学习,共同进步 ===================



















本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366440,如需转载请自行联系原作者
相关文章
|
3月前
Qt6学习笔记十一(计时器事件)
Qt6学习笔记十一(计时器事件)
26 0
Qt6学习笔记十一(计时器事件)
|
5月前
|
小程序 前端开发 JavaScript
小程序计时器
小程序计时器
80 0
倒计时功能制作
今天制作一个商城项目距离抢购还剩多长时间的一个小功能 首先要知道这个倒计时是不断的变化的,所以需要用到定时器来自动变化(setInterval),有三个标签存放时分秒,再利用innerHTML将计算出来的时间放入三个标签内,第一次执行也是间隔毫秒数,因此刷新页面会有空白,我们最好采取封装函数的方式,这样可以先调用一次这个函数,防止刚开始刷新页面有空白问题。
|
11月前
|
存储 缓存 小程序
如何实现游戏中的在线计时器和离线计时器
本文包含了游戏中两种计时器的实现原理和实现方法,皆在帮助你彻底的搞懂游戏开发中的计时器。 如果你没有任何的游戏开发经验,欢迎观看我的“人人都能做游戏”系列视频教程,它会手把手的教你做出自己的第一个小游戏。 在游戏中经常会有需要倒计时的需求,例如倒计时 10 分钟可以获得 1 点体力,倒计时 1 小时后可以开启一个宝箱,或者是根据游戏的计时获得奖励等等。
217 0
|
前端开发
前端学习案例1-计时器1
前端学习案例1-计时器1
45 0
前端学习案例1-计时器1
|
前端开发
前端学习案例2-计时器2
前端学习案例2-计时器2
35 0
前端学习案例2-计时器2
|
前端开发
前端学习案例3-计时器3
前端学习案例3-计时器3
55 0
前端学习案例3-计时器3
|
前端开发 JavaScript
如何用最简单的代码制作带定时器的轮播图
如何用最简单的代码制作带定时器的轮播图 前几天写了一篇有关轮播图制作的博客,但当时没有添加定时的效果,说白了就是没有自动轮播的效果,今天来说一下怎样添加自动轮播效果。如图: 在这里插入图片描述 HTML代码: &lt;div class=&quot;box&quot; id=&quot;box&quot;&gt; &lt;img src=&quot;img/1.jpg&quot; alt=&quot;&quot;&gt; &lt;!-- &lt;img src=&quot;img/1.jpg&quot; alt=&quot;&quot;&gt; &lt;img src=&quot;img/2.jpg&quot; alt=&quot;&quot;&gt; &lt;img src=&quot;img/3.jpg&quot; alt=&quot;&quot;&gt; --&gt;
如何用最简单的代码制作带定时器的轮播图
实现计时器和倒计时工具(Unity3D)
今天分享一下如何基于Unity3D做计时器工具,为了方便演示,使用了UGUI的Text,代码简单具有拓展性,然后有什么错误或者意见也欢迎大家给我提出来。微信二维码已经显示在博客主页,有想要沟通学习的,项目外包的都可以加一下。 分享一下我另一篇关于时间计时的文章: 【Unity3D】获取到游戏时间,并显示出来