AJ学IOS(10)UI之_NSTimer_ios计时器
AJ分享,必须精品
先看效果
代码
#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为多少来加入事件
}
AJ学IOS(10)UI之_NSTimer_ios计时器的更多相关文章
- AJ学IOS(28)UI之Quartz2D简单介绍
AJ分享,必须精品 iOS开发UI篇—Quartz2D简单介绍 什么是Quartz2D Quartz 2D是⼀个二维绘图引擎,同时支持iOS和Mac系统 Quartz 2D能完成的工作: 绘制图形 : ...
- AJ学IOS(13)UI之UITableView学习(下)汽车名牌带右侧索引
AJ分享,必须精品 先看效果图 代码 ViewController #import "NYViewController.h" #import "NYCarGroup.h& ...
- AJ学IOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController
AJ分享,必须精品 一:添加导航控制器 上一篇博客完成了对底部的TabBar的设置,这一章我们完成自定义导航控制器(NYNavigationController). 为啥要做自定义呢,因为为了更好地封 ...
- AJ学IOS(01) UI之Hello World与加法计算器
不多说,AJ分享,必须精品 这两个一个是HelloWorld(左边) 另一个是 加法计算器(右边)的截图. 先运行第一个 程序看看效果 1.打开Xcode(没有哦mac系统的没有xcode的帮你们默哀 ...
- AJ学IOS(42)UI之核心动画CAAnimationGroup以及其他
AJ分享,必须精品 效果: 代码: 很简单,不多说,就是把一堆动画放一起,看代码. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent * ...
- AJ学IOS(37)UI之CALayer
AJ分享,必须精品 CALayer 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实UIView之所以能 ...
- AJ学IOS(35)UI之Quartz2D仿真支付宝手势解锁_代理获得密码。
AJ分享,必须精品 效果: 实现步骤 其实这个实现起来不难 第一步先放好主要的UI,一张背景图和一个View 第二部就是把9个button放到view中,设置好按钮的默认和选中图片. 注意:创建时候的 ...
- AJ学IOS(27)UI之iOSUIKit字符属性NSAttributedString概述
AJ分享,必须精品 UIKit字符属性NSAttributedString概述 字符属性 字符属性可以应用于 attributed string 的文本中. NSString *const NSFon ...
- AJ学IOS(23)UI之控制器管理
AJ分享,必须精品 控制器以及view的多种创建方式 控制器view的加载 通过storyboard创建 1:先加载storyboard⽂件(Test是storyboard的⽂文件名) UIStory ...
随机推荐
- thinkphp 前后端分离
thinkphp 前后端分离 简单记录一下之前学习tp的历程吧. 前端HTML页面渲染 <?php namespace app\index\controller; use think\Contr ...
- MySQL学习(5)
三 触发器 对某个表进行某种操作(如:增删改查),希望触发某个动作,可以使用触发器. 1.创建触发器 create trigger trigger1_before_insert_tb1 before ...
- 动态网站项目(Dynamic Web Project)CRUD(增删改查)功能的实现(mvc(五层架构)+jdbc+servlet+tomcat7.0+jdk1.8),前端使用JSP+JSTL+EL组合
代码分享链接 https://pan.baidu.com/s/1UM0grvpttHW9idisiqa6rA 提取码:hx7c 图示 项目结构 1.SelectAllUser ...
- 用序列到序列和注意模型实现的:Translation with a Sequence to Sequence Network and Attention
In this project we will be teaching a neural network to translate from French to English. 最后效果: [KEY ...
- ES6全面讲解
写在之前.讲解了比较常用的ES6知识点,可以快速的入门.有两个比较复杂的知识点(promise,generator)则是留在了其他文章中,在里面详细讲解. 介绍 1.历史 1.ECMAScript ...
- [leetcode] 树 -Ⅰ
均为 Simple 难度的水题. 二叉树的中序遍历 题目[94]:给定一个二叉树,返回它的中序 遍历. 解题思路:Too simple. class Solution { public: vector ...
- 文件输入输出实例&Ptask的编写
前言 最近在写Ptask,顺便了解了如何进行文件读入输出.而在Ptask中最重要,也是最最容易出bug的地方就是文件操作.那么如何进行文件输入输出,在程序中起到重要作用呢? 输入 首先为了保证可以在控 ...
- CAP定理和BASE理论
CAP定理和BASE理论 标签(空格分隔): 操作系统 CAP定理 CAP定理: 一个分布式系统最多只能满足一致性 (Consistency), 可用性(Availability)和分区容错性(Par ...
- #VScodd集成Git Bash 命令行 #怎么把Git Bash集成到VScode
配置 Step1. File-Preferences-Setting Step2. 搜索"terminal>integrated>shell A" Step3. 找到t ...
- springboot 启动时执行方法
Springboot提供了两种“开机启动”某些方法的方式:ApplicationRunner和CommandLineRunner.下面简单介绍下ApplicationRunner 1.创建个Tests ...