NSTimer
2015-10-26 13:18
253 查看
//创建NSTimer
NSTimer*timer = [[NSTimeralloc]init];
//每1秒运行一次function方法。
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(function:) userInfo:nil
repeats:YES];
//TimeInterval:调用的时间间隔
//repeats:决定是否重复调NSTimer
//注意:将计数器的repeats设置为YES的时候,self的引用计数会加1。因此可能会导致self(即viewController)不能release,所以,必须在viewWillAppear的时候,将计数器timer停止,否则可能会导致内存泄露。
//停止timer的运行,但这个是永久的停止:(注意:停止后,一定要将timer赋空,否则还是没有释放。
//取消定时器
[timer invalidate];
timer =
nil;
//要想实现:先停止,然后再某种情况下再次开启运行timer,可以使用下面的方法:
//暂时关闭定时器
[myTimer setFireDate:[NSDate
distantFuture]];
//开启定时器
[myTimer setFireDate:[NSDate
distantPast]];
//设置启动页面时间
NSThread sleepForTimeInterval:<#(NSTimeInterval)#>
/* NSTimer.h
Copyright (c) 1994-2014, Apple Inc. All rights reserved.
*/
#import
<Foundation/NSObject.h>
#import
<Foundation/NSDate.h>
@interface
NSTimer :
NSObject
+ (NSTimer
*)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation
*)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer
*)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation
*)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer
*)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget
selector:(SEL)aSelector userInfo:(id)userInfo
repeats:(BOOL)yesOrNo;
+ (NSTimer
*)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget
selector:(SEL)aSelector userInfo:(id)userInfo
repeats:(BOOL)yesOrNo;
- (instancetype)initWithFireDate:(NSDate
*)date interval:(NSTimeInterval)ti target:(id)t
selector:(SEL)s userInfo:(id)ui
repeats:(BOOL)rep
NS_DESIGNATED_INITIALIZER;
- (void)fire;
@property
(copy)
NSDate
*fireDate;
@property
(readonly)
NSTimeInterval
timeInterval;
// Setting a tolerance for a timer allows it to fire later than the scheduled fire date, improving the ability of the system to optimize for increased power savings and responsiveness.
The timer may fire at any time between its scheduled fire date and the scheduled fire date plus the tolerance. The timer will not fire before the scheduled fire date. For repeating timers, the next fire date is calculated from the original fire date regardless
of tolerance applied at individual fire times, to avoid drift. The default value is zero, which means no additional tolerance is applied. The system reserves the right to apply a small amount of tolerance to certain timers regardless of the value of this property.
// As the user of the timer, you will have the best idea of what an appropriate tolerance for a timer may be. A general rule of thumb, though, is to set the tolerance to at least 10%
of the interval, for a repeating timer. Even a small amount of tolerance will have a significant positive impact on the power usage of your application. The system may put a maximum value of the tolerance.
@property
NSTimeInterval
tolerance
NS_AVAILABLE(10_9,
7_0);
- (void)invalidate;
@property
(readonly,
getter=isValid)
BOOL
valid;
@property
(readonly,
retain)
id
userInfo;
@end
NSTimer*timer = [[NSTimeralloc]init];
//每1秒运行一次function方法。
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(function:) userInfo:nil
repeats:YES];
//TimeInterval:调用的时间间隔
//repeats:决定是否重复调NSTimer
//注意:将计数器的repeats设置为YES的时候,self的引用计数会加1。因此可能会导致self(即viewController)不能release,所以,必须在viewWillAppear的时候,将计数器timer停止,否则可能会导致内存泄露。
//停止timer的运行,但这个是永久的停止:(注意:停止后,一定要将timer赋空,否则还是没有释放。
//取消定时器
[timer invalidate];
timer =
nil;
//要想实现:先停止,然后再某种情况下再次开启运行timer,可以使用下面的方法:
//暂时关闭定时器
[myTimer setFireDate:[NSDate
distantFuture]];
//开启定时器
[myTimer setFireDate:[NSDate
distantPast]];
//设置启动页面时间
NSThread sleepForTimeInterval:<#(NSTimeInterval)#>
/* NSTimer.h
Copyright (c) 1994-2014, Apple Inc. All rights reserved.
*/
#import
<Foundation/NSObject.h>
#import
<Foundation/NSDate.h>
@interface
NSTimer :
NSObject
+ (NSTimer
*)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation
*)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer
*)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation
*)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer
*)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget
selector:(SEL)aSelector userInfo:(id)userInfo
repeats:(BOOL)yesOrNo;
+ (NSTimer
*)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget
selector:(SEL)aSelector userInfo:(id)userInfo
repeats:(BOOL)yesOrNo;
- (instancetype)initWithFireDate:(NSDate
*)date interval:(NSTimeInterval)ti target:(id)t
selector:(SEL)s userInfo:(id)ui
repeats:(BOOL)rep
NS_DESIGNATED_INITIALIZER;
- (void)fire;
@property
(copy)
NSDate
*fireDate;
@property
(readonly)
NSTimeInterval
timeInterval;
// Setting a tolerance for a timer allows it to fire later than the scheduled fire date, improving the ability of the system to optimize for increased power savings and responsiveness.
The timer may fire at any time between its scheduled fire date and the scheduled fire date plus the tolerance. The timer will not fire before the scheduled fire date. For repeating timers, the next fire date is calculated from the original fire date regardless
of tolerance applied at individual fire times, to avoid drift. The default value is zero, which means no additional tolerance is applied. The system reserves the right to apply a small amount of tolerance to certain timers regardless of the value of this property.
// As the user of the timer, you will have the best idea of what an appropriate tolerance for a timer may be. A general rule of thumb, though, is to set the tolerance to at least 10%
of the interval, for a repeating timer. Even a small amount of tolerance will have a significant positive impact on the power usage of your application. The system may put a maximum value of the tolerance.
@property
NSTimeInterval
tolerance
NS_AVAILABLE(10_9,
7_0);
- (void)invalidate;
@property
(readonly,
getter=isValid)
BOOL
valid;
@property
(readonly,
retain)
id
userInfo;
@end
相关文章推荐
- 上海酒店分布数据接口分享
- Android 使用Websocket
- mapreduce运行的5个阶段
- UIPageControl
- Codeforces Round #327 (Div. 2) E. Three States BFS
- Windows五个网络IO模型(服务器用)
- ngix使用
- CPU占用率问题
- jQuery学习之旅 Item4 细说DOM操作
- oleload导excel数据
- 关于button的用法
- ubuntu设置静态ip
- jQuery学习之旅 Item4 细说DOM操作
- swift开发笔记14 - 解析json数据文件
- HADOOP(2.4)+HBASE(0.98.1)高可用集群详细部署
- SharePoint 2013 IT Professional--巧用Navigation Audience
- 调用系统自带浏览器的方法
- target action
- 一些面试题
- 人一生要做的一百件事