NSTimer - 1
2015-10-08 16:02
411 查看
NSTimer 分别分基础篇和深入篇,深入篇会设计到NSRunloop所以现在先不详述,会在NSTimer - 2 篇里面学习。
基础篇也是非常好学习的,因为NSTimer提供的接口方法少而已很少理解和使用。
初始化 和 使用也是非常简单,例如:
_hideFlowButtonTimer = [NSTimer timerWithTimeInterval:4.0f target:self selector:@selector(hideFlowButton) userInfo:nil repeats:NO];
还可以立即触发,使用方法:
- (void)fire;
防止内存泄漏,关闭计时器:
[timer invalidate];
timer = nil;
还可以灵活地开启 、 关闭:
//页面将要进入前台,开启定时器
-(void)viewWillAppear:(BOOL)animated
{
//开启定时器
[scrollView.myTimer setFireDate:[NSDate distantPast]];
}
//页面消失,进入后台不显示该页面,关闭定时器
-(void)viewDidDisappear:(BOOL)animated
{
//关闭定时器
[scrollView.myTimer setFireDate:[NSDate distantFuture]];
}
而在参考文章 Reference - 1中提到一点没有想到的计时器和函数方法相关对象(Target)里面对象的retain关系是怎么样的,详细查看Reference - 1。
这里给出一个计时器结合UIView的动画的简单实现:
在moveFlowButton初始化定时器:
- (void)moveFlowButton
{
//洋葱头缩进到屏幕边缘定时器
_hideFlowButtonTimer = [NSTimer timerWithTimeInterval:4.0f target:self selector:@selector(hideFlowButton) userInfo:nil repeats:NO];
float durationTime = _flowButton.superview.bounds.size.width * 0.00125f ; //动画持续时间
if (_flowButton.center.x < _flowButton.superview.bounds.size.width / 2) {
[UIView animateWithDuration:durationTime animations:^{
_flowButton.center = CGPointMake(_flowButton.bounds.size.width / 2, _flowButton.center.y);
_flowMenu.center = CGPointMake(_flowMenu.bounds.size.width / 2 + _flowButton.bounds.size.width, _flowMenu.center.y - 5);
} completion:^(BOOL finished){
[_hideFlowButtonTimer fire];
}];
}
}
定时器相关的方法实现:
- (void)hideFlowButton
{
if (_flowButton.center.x < _flowButton.superview.bounds.size.width / 2) {
[UIView animateWithDuration:0.5f animations:^{
_flowButton.frame = CGRectMake(-_flowButton.bounds.size.width + 10, _flowButton.frame.origin.y, _flowButton.bounds.size.width, _flowButton.bounds.size.height);
} completion:^(BOOL finished){
_isFlowButtonDismiss = YES;
}];
}
}
参考文章:
Reference - 1 : 定时器的关闭和开启 :http://blog.csdn.net/enuola/article/details/8099461
Reference - 2 : 全面深入定时器 - target对象的retain值 、定时器的延时 、 与runloop的关系 : http://blog.csdn.net/enuola/article/details/9163051#comments
基础篇也是非常好学习的,因为NSTimer提供的接口方法少而已很少理解和使用。
初始化 和 使用也是非常简单,例如:
_hideFlowButtonTimer = [NSTimer timerWithTimeInterval:4.0f target:self selector:@selector(hideFlowButton) userInfo:nil repeats:NO];
还可以立即触发,使用方法:
- (void)fire;
防止内存泄漏,关闭计时器:
[timer invalidate];
timer = nil;
还可以灵活地开启 、 关闭:
//页面将要进入前台,开启定时器
-(void)viewWillAppear:(BOOL)animated
{
//开启定时器
[scrollView.myTimer setFireDate:[NSDate distantPast]];
}
//页面消失,进入后台不显示该页面,关闭定时器
-(void)viewDidDisappear:(BOOL)animated
{
//关闭定时器
[scrollView.myTimer setFireDate:[NSDate distantFuture]];
}
而在参考文章 Reference - 1中提到一点没有想到的计时器和函数方法相关对象(Target)里面对象的retain关系是怎么样的,详细查看Reference - 1。
这里给出一个计时器结合UIView的动画的简单实现:
在moveFlowButton初始化定时器:
- (void)moveFlowButton
{
//洋葱头缩进到屏幕边缘定时器
_hideFlowButtonTimer = [NSTimer timerWithTimeInterval:4.0f target:self selector:@selector(hideFlowButton) userInfo:nil repeats:NO];
float durationTime = _flowButton.superview.bounds.size.width * 0.00125f ; //动画持续时间
if (_flowButton.center.x < _flowButton.superview.bounds.size.width / 2) {
[UIView animateWithDuration:durationTime animations:^{
_flowButton.center = CGPointMake(_flowButton.bounds.size.width / 2, _flowButton.center.y);
_flowMenu.center = CGPointMake(_flowMenu.bounds.size.width / 2 + _flowButton.bounds.size.width, _flowMenu.center.y - 5);
} completion:^(BOOL finished){
[_hideFlowButtonTimer fire];
}];
}
}
定时器相关的方法实现:
- (void)hideFlowButton
{
if (_flowButton.center.x < _flowButton.superview.bounds.size.width / 2) {
[UIView animateWithDuration:0.5f animations:^{
_flowButton.frame = CGRectMake(-_flowButton.bounds.size.width + 10, _flowButton.frame.origin.y, _flowButton.bounds.size.width, _flowButton.bounds.size.height);
} completion:^(BOOL finished){
_isFlowButtonDismiss = YES;
}];
}
}
参考文章:
Reference - 1 : 定时器的关闭和开启 :http://blog.csdn.net/enuola/article/details/8099461
Reference - 2 : 全面深入定时器 - target对象的retain值 、定时器的延时 、 与runloop的关系 : http://blog.csdn.net/enuola/article/details/9163051#comments
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- 举例详解iOS开发过程中的沙盒机制与文件
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器
- IOS开发第三方语音-微信语音
- iOS开发之路--微博OAuth授权_取得用户授权的accessToken
- 基于UIControl控件实现ios点赞功能
- iOS开发中实现邮件和短信发送的简单示例