//创建
NSTimer
NSTimer
*timer = [[
NSTimer
alloc
]
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 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;
*/
#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