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
====================== 相互学习,共同进步 ===================
转载于:https://blog.51cto.com/dingxiaowei/1366440
2013-8-9练习[多种方法制作一个计时器]相关推荐
- 使用 history 对象和 location 对象中的属性和方法制作一个简易的网页浏览工具
查看本章节 查看作业目录 需求说明: 使用 history 对象和 location 对象中的属性和方法制作一个简易的网页浏览工具 实现思路: 使用history对象中的 forward() 方法和 ...
- java线程写个计时器_用Java开发C/S应用(二):启动新的线程,制作一个计时器...
接着上面的例子,我们来制作一个计时器,呃,这个例子或许有些难,我会讲的尽量详细 我先来介绍启动新线程的方法: Runnable runnable=newRunnable() {publicvoidru ...
- python制作一个计时器_如何在python中创建一个反应计时器作为GUI?
我有一个反应计时器的问题.我想在我的程序中有一个按钮,它能告诉我从定时器开始按下按钮所花的时间.在 假设我打开我的程序,我想要一个按钮,如果我点击它,它将打印出计时器启动后我按下它所花的时间.然后在我 ...
- python制作一个计时器_用Python制作一个猫咪小秒表
Python基础视频教程 入门免费观看-创客学院www.makeru.com.cn 秒表是一项随处可见的神奇小物件,最常用到秒表的两大场景,一个是运动会,另一个是健身房,因此也总是让人联想到汗水和心 ...
- html靶心制作,用PS单色填充制作一个靶心
使用单色填充的方法制作一个靶心,属于Photoshop的入门课程,好了,喜欢的朋友可以参考本文! 步骤 1.新建图像文档.选择"文件-新建"命令,在弹出的对话框中设置"预 ...
- 【小制作】使用NE555和一个计数器制作一个流水灯
流水灯就像是"Hello World"一样存在于单片机教程之中,但是今天要用纯硬件的方法制作一个流水灯.所用的器件就是题目上的那两个器件.NE555和一个计数器,计数器选用的是CD ...
- c# asp.net core取当月第一天和最后一天及删除最后一个字符的多种方法
当月第一天0时0分0秒 DateTime.Now.AddDays(1 - DateTime.Now.Day).Date 当月最后一天23时59分59秒 DateTime.Now.AddDays(1 - ...
- php跳转到另外一个方法,PHP 页面跳转到另一个页面的多种方法方法总结
PHP 页面跳转到另一个页面的多种方法方法总结 一用 HTTP 头信息 也就是用 PHP 的 HEADER 函数 PHP 里的 HEADER 函数的作用就是向浏览器发出由 HTTP 协议规定的本来应该 ...
- [html] 制作一个不规则形状有哪些方法可以实现?
[html] 制作一个不规则形状有哪些方法可以实现? canvas 个人简介 我是歌谣,欢迎和大家一起交流前后端知识.放弃很容易, 但坚持一定很酷.欢迎大家一起讨论 主目录 与歌谣一起通关前端面试题
最新文章
- 树莓派Raspberry Pi 16G SD卡刷系统时默认空间扩容
- android 阿里云 maven,Android SDK接入(Maven集成)
- java高并发(一)导学
- redis之rehash原理
- 多个DataSet数据合并
- MVC常见的控制器,接口,数据层之间的操作
- Magento 获取分类的父分类和子分类
- python常用小技巧_Python 实用技巧
- gogs 把用户加入团队
- JAVA继承类初始化顺序
- java静态代理和动态代理的区别
- 单个次序统计量的密度函数
- 美国大学计算机软件专业排名,美国大学计算机专业排名
- linux系统编程之进程通信
- 实对称矩阵的若干性质与详细证明
- staf linux运行模式,IBM 自动化测试框架STAF介绍
- html涟漪动画效果,CSS 在按钮上做个涟漪效果(Ripple Animation)
- 四羧基锌酞菁(ZnC4Pc),Zn-taPc 酞菁类化合物,齐岳生物供应酞菁材料
- Double的compareTo
- nprogress 插件 网页顶部加载进度条