猫猫学IOS(十)UI之_NSTimer_ios计时器
2015-03-25 21:13
337 查看
猫猫分享,必须精品
素材代码地址:/article/1326235.html
原文地址:http://blog.csdn.net/u013357243?viewmode=contents
![](http://img.blog.csdn.net/20150325210428784)
![](http://img.blog.csdn.net/20150325210525641)
![](http://img.blog.csdn.net/20150325210538496)
![](http://img.blog.csdn.net/20150325210515163)
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]
参数说明:
1,时间间隔,double
2,监听时钟触发的对象
3,调用方法
4,userInfo,可以是任意对象,通常传递nil,如果有传递值,大多数是为了在多个计数器中分辨用哪个
5,repeats:是否重复执行调用方法。
是否要在发生滚动事件时候继续计时器
将timer添加到运行循环,模式:NSRunLoopCommonModes监听滚动模式
[[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode];
[[[UIAlertView alloc] initWithTitle:@”开始” message:@”开始啦。。。” delegate:self cancelButtonTitle:@”取消” otherButtonTitles:@”确定”, nil] show];
AlertView 中输入的最后是数组,可以通过代理方式来实现方法
ps:新建iOS交流学习群:304570962
可以加猫猫QQ:1764541256 或则微信znycat
让我们一起努力学习吧。
原文:http://blog.csdn.net/u013357243?viewmode=contents
素材代码地址:/article/1326235.html
原文地址:http://blog.csdn.net/u013357243?viewmode=contents
先看效果
代码
原文地址:http://blog.csdn.net/u013357243?viewmode=contents // // NYViewController.m // 05 - 倒计时 // // Created by apple on 15-3-25. // Copyright (c) 2015年 znycat. All rights reserved. // #import "NYViewController.h" @interface NYViewController () <UIAlertViewDelegate> @property (weak, nonatomic) IBOutlet UILabel *counterLabel; @property (nonatomic, strong) NSTimer *timer; @end @implementation NYViewController /**开始*/ -(IBAction)start{ // 倒计时10秒,计时器 /* NSTimer scheduledTimerWithTimeInterval 参数说明: 1,时间间隔,double 2,监听时钟触发的对象 3,调用方法 4,userInfo,可以是任意对象,通常传递nil,如果有传递值,大多数是为了在多个计数器中分辨用哪个 5,repeats:是否重复执行调用方法。 */ // scheduledTimerWithTimeInterval 方法本质上就是创建一个时钟, // 添加到运行循环的模式是DefaultRunloopMode // _________________________________________________________________________________ //1> // self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; // 2> 与1一样 // self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; // //将timer添加到运行循环,模式:默认运行循环模式 // [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode]; // __________________________________________________________________________________ //3> self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; //将timer添加到运行循环,模式:NSRunLoopCommonModes监听滚动模式 [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; } /**每秒更新counterLabel属性*/ -(void) updateTimer { //1,取出标签中得数字 int counter = self.counterLabel.text.intValue; //2,判断是否为0,如果是则停止时钟 if (--counter<0) { //提示用户,提示框 [[[UIAlertView alloc] initWithTitle:@"开始" message:@"开始啦。。。" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show]; //AlertView 中输入的最后是数组,可以通过代理方式来实现方法 [self pause]; }else{ //,3修改数字并显示更新UILabel self.counterLabel.text = [NSString stringWithFormat:@"%d",counter]; } } /**暂停*/ -(IBAction)pause { //停止时钟,invalidate是唯一的方法,一调用就干掉timer了,想再用只能重新实例化 [self.timer invalidate]; } -(IBAction)stop { [self pause]; self.counterLabel.text = @"10"; } #pragma mark - alertView 代理方法 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { NSLog(@"%d",buttonIndex); } @end
注意点NSTimer
用法:self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]
参数说明:
1,时间间隔,double
2,监听时钟触发的对象
3,调用方法
4,userInfo,可以是任意对象,通常传递nil,如果有传递值,大多数是为了在多个计数器中分辨用哪个
5,repeats:是否重复执行调用方法。
是否要在发生滚动事件时候继续计时器
将timer添加到运行循环,模式:NSRunLoopCommonModes监听滚动模式
[[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode];
提示框 UIAlertView
提示框[[[UIAlertView alloc] initWithTitle:@”开始” message:@”开始啦。。。” delegate:self cancelButtonTitle:@”取消” otherButtonTitles:@”确定”, nil] show];
AlertView 中输入的最后是数组,可以通过代理方式来实现方法
#pragma mark - alertView 代理方法 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { NSLog(@"%d",buttonIndex); //0指的是取消按钮 //可以加入if判断buttonIndx为多少来加入事件 }
ps:新建iOS交流学习群:304570962
可以加猫猫QQ:1764541256 或则微信znycat
让我们一起努力学习吧。
原文:http://blog.csdn.net/u013357243?viewmode=contents
相关文章推荐
- (素材源码)猫猫学IOS(十)UI之_NSTimer_ios计时器
- 猫猫学IOS(十)UI之_NSTimer_ios计时器
- (素材源码)猫猫学IOS(十)UI之_NSTimer_ios计时器
- AJ学IOS(10)UI之_NSTimer_ios计时器
- 猫猫学IOS(五)UI之360等下载管理器九宫格UI
- 猫猫学IOS(五)UI之360等下载管理器九宫格UI
- 【学习ios之路:UI系列】实现轮播图效果(UIImageView,UIScrollView,UIPageControl,NSTimer相结合)
- 猫猫学IOS(一)UI之Hello World与加法计算器
- IOS 计时器 NSTimer
- (素材源码)猫猫学IOS(七)UI之UITextField代理事件_类似QQ登陆窗口的简单实现
- 猫猫学IOS(四)UI之半小时搞定Tom猫
- IOS 计时器 NSTimer
- 猫猫学IOS(八)UI之热门_喜马拉雅UI实现-UIScrollView的使用
- 猫猫学IOS(七)UI之UITextField代理事件_类似QQ登陆窗口的简单实现
- 猫猫学IOS(七)UI之UITextField代理事件_类似QQ登陆窗口的简单实现
- (素材_源码) 猫猫学IOS(五)UI之360等下载管理器九宫格UI
- iOS - 计时器NSTimer 和CADisplayLink用法
- IOS 计时器 NSTimer
- 猫猫学IOS(二)UI之按钮操作 点击变换 移动 放大缩小 旋转
- (素材源码)猫猫学IOS(六)UI之iOS热门游戏_超级猜图