UI NSTimer
来源:互联网 发布:c语言输出文本文件 编辑:程序博客网 时间:2024/05/17 23:12
- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. // scheduledTimerWithTimeInterval:(NSTimeInterval)seconds// 预订一个Timer,设置一个时间间隔。// 表示输入一个时间间隔对象,以秒为单位,一个>0的浮点类型的值,如果该值<0,系统会默认为0.1// target:(id)aTarget// 表示发送的对象,如self// selector:(SEL)aSelector// 方法选择器,在时间间隔内,选择调用一个实例方法// userInfo:(id)userInfo// 此参数可以为nil,当定时器失效时,由你指定的对象保留和释放该定时器。// repeats:(BOOL)yesOrNo// 当YES时,定时器会不断循环直至失效或被释放,当NO时,定时器会循环发送一次就失效。// invocation:(NSInvocation *)invocation // //关于定时器的以下信息// Information About a Timer// – isValid //是否在运行// – fireDate //Returns the date at which the receiver will fire.// – setFireDate: //重新设置定时器开始运行的时间// – timeInterval //定时器延时时间// – userInfo //其他信息 // UIButton *startButton=[UIButton buttonWithType:UIButtonTypeSystem];// startButton.frame=CGRectMake(50, 70, 100, 50);// [startButton setTitle:@"开始" forState:UIControlStateNormal];// [startButton addTarget:self action:@selector(startButtonAction:) forControlEvents:UIControlEventTouchUpInside];// [self.view addSubview:startButton];// // UIButton *endButton=[UIButton buttonWithType:UIButtonTypeSystem];// endButton.frame=CGRectMake(50, 150, 100, 50);// [endButton setTitle:@"结束" forState:UIControlStateNormal];// [endButton addTarget:self action:@selector(endButtonAction:) forControlEvents:UIControlEventTouchUpInside];// [self.view addSubview:endButton]; }- (void)timerAction:(NSTimer*)timer{ NSLog(@"timerAction");}- (void)startButtonAction:(UIButton*)button{ //创建NSTimer对象 NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES]; self.timer=timer; }- (void)endButtonAction:(UIButton*)button{ if (_timer) { [_timer invalidate]; _timer=nil; }}-(void)dealloc{ [_timer release]; [_timeDisply release]; [super dealloc];}- (IBAction)startTime:(UIButton *)sender { if (!_timer) { _timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTimeAtTimeDisplay) userInfo:nil repeats:YES]; } }- (void)changeTimeAtTimeDisplay{ int second=[self.timeDisply.text intValue]; self.timeDisply.text=[NSString stringWithFormat:@"%d",second-1];}- (IBAction)stopTime:(UIButton *)sender { if (_timer) { NSLog(@"调用 self.time为真!!"); //如果定时器在运行 if ([self.timer isValid]) { NSLog(@"单击停止按钮,取消定时器!!"); [self.timer invalidate]; //这行代码很关键 _timer=nil; } }}
0 0
- UI NSTimer
- UI-NSTimer介绍
- UI基础学习之(十六):NSTimer
- NSTimer 倒计时自动登录,每秒更新UI
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- PHP学习
- 拓扑排序_TopologicalSort.c
- Unity3D 4.6.3之浅拷贝与深拷贝
- PHP网络
- 文章标题
- UI NSTimer
- PHP文件处理
- 【Web】ServletContext与ServletConfig
- hdoj 1518 Square 【dfs+剪枝】
- 文件及文件夹功能
- Linux高级编程复习笔记 第三章 makefile IO read write
- 山东第二届省赛回顾 Binomial Coeffcients
- 工厂模式----我要吃中式披萨
- PHP学习2