iOS学习 - scrollView(时钟)
2015-08-28 11:10
591 查看
// // YJViewController.m // 07-scrollView-倒计时 // // Created by JACKY-MAC on 15-6-17. // Copyright (c) 2015年 www.train.com. All rights reserved. // #import "YJViewController.h" @interface YJViewController ()<UIAlertViewDelegate> @property (weak, nonatomic) IBOutlet UILabel *counterLabel; @property(nonatomic,strong)NSTimer *timer; @end @implementation YJViewController /** 开始 */ - (IBAction)start { // 倒计时10秒,每秒更新一下Label的显示 // 计时器 /** 参数说明 1. 时间间隔,double 2. 监听时钟触发的对象 3. 调用方法 4. userInfo,可以是任意对象,通常传递nil 5. repeats:是否重复 */ self.counterLabel.text = @"10"; // scheduledTimerWithTimeInterval 方法本质上就是创建一个时钟, // 添加到运行循环的模式是DefaultRunLoopMode // ---------------------------------------------- // 1> // self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:@"hello timer" 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:NSDefaultRunLoopMode]; } /** 时钟更新方法 */ - (void)updateTimer:(NSTimer *)timer { // 1. 取出标签中的数字 int counter = self.counterLabel.text.intValue; // 2. 判断是否为零,如果为0,停止时钟 if (--counter<0) { // 停止时钟 [self pause]; // 提示用户,提示框 [[[UIAlertView alloc] initWithTitle:@"开始" message:@"开始啦........" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil] show]; }else { // 3. 修改数字并更新UI self.counterLabel.text = [NSString stringWithFormat:@"%d",counter]; } } /** 暂停 */ - (IBAction)pause { // 停止时钟,invalidate是唯一停止时钟的方法 // 一旦调用了invalidate方法,timer就无效了,如果再次启动时钟,需要重新实例化 [self.timer invalidate]; } #pragma mark - alertView代理方法 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"%ldaaa-------", (long)buttonIndex); } @end
相关文章推荐
- 关于iOS多线程
- iOS9AdaptationTips
- 苹果开发者中心关于邓白氏号码的答复
- iOS_时间戳与日期转换(几分钟前)
- 谈谈关于GCD相关问题
- Why BIOS loads MBR into 0x7C00 in x86 ?
- iOS 使用ZBarSDK扫描条形码和二维码
- iOS AutoLayout自动布局中级开发教程(7)-底部顶部baseLine基线对齐
- iOS学习 - scrollView(图片查看器)
- ios NSString 转 float的注意
- iOS_Json字符串转字典、字典转成Json字符串
- iOS开发-OC-类、对象、方法
- iOS开发 - 内存静态分析
- iOS-界面搭建鼻祖_九宫格算法
- ios日期格式转换
- iOS开发中遇到的头文件找不到的问题解决办法
- iOS多线程之NSoperation和GCD的比较
- ios获取当前屏幕所显示Controller
- iOS 后台播放音乐
- iOS开发多线程篇—多线程简单介绍