NSTimer 定时器简单用法

一、初始化方法:有五种初始化方法,分别是

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;

使用方法:

  - (void)viewDidLoad {

[super viewDidLoad];

//初始化一个Invocation对象

NSInvocation * invo = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(init)]];

[invo setTarget:self];

[invo setSelector:@selector(myLog)];

NSTimer * timer = [NSTimer timerWithTimeInterval:1 invocation:invo repeats:YES];

//加入主循环池中

[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];

//开始循环

[timer fire];

 }
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;

使用方法:

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 invocation:invo repeats:YES];
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

使用方法:

NSTimer * timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(myLog) userInfo:nil repeats:NO]

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

使用方法:

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myLog:) userInfo:@"123" repeats:YES]

- (instancetype)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(id)ui repeats:(BOOL)rep 

使用方法:

NSTimer * timer = [[NSTimer alloc]initWithFireDate:[NSDate distantPast] interval:1 target:self selector:@selector(myLog:) userInfo:nil repeats:YES];

[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];

注意:这五种初始化方法的异同:

    1、参数repeats是指定是否循环执行,YES将循环,NO将只执行一次。

    2、timerWithTimeInterval这两个类方法创建出来的对象如果不用 addTimer: forMode方法手动加入主循环池中,将不会循环执行。并且如果不手动调用fair,则定时器不会启动。

    3、scheduledTimerWithTimeInterval这两个方法不需要手动调用fair,会自动执行,并且自动加入主循环池。

    4、init方法需要手动加入循环池,它会在设定的启动时间启动。

二、成员变量

@property (copy) NSDate *fireDate;

这是设置定时器的启动时间,常用来管理定时器的启动与停止

//启动定时器

timer.fireDate = [NSDate distantPast];

//停止定时器

timer.fireDate = [NSDate distantFuture];

@property (readonly) NSTimeInterval timeInterval;

这个是一个只读属性,获取定时器调用间隔时间。

@property NSTimeInterval tolerance;

这是7.0之后新增的一个属性,因为NSTimer并不完全精准,通过这个值设置误差范围。

@property (readonly, getter=isValid) BOOL valid;

获取定时器是否有效

@property (readonly, retain) id userInfo;

获取参数信息

三、关于内存释放

如果我们启动了一个定时器,在某个界面释放前,应该将这个定时器停止,仅仅置为nil是不够的,原因是系统的循环池中还保有这个对象。所以我们需要这样做:

-(void)dealloc{

NSLog(@"dealloc:%@",[self class]);

}

- (void)viewDidLoad {

[super viewDidLoad];

timer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myLog:) userInfo:nil repeats:YES];

UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];

btn.backgroundColor=[UIColor redColor];

[btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

- (void)btn{

if (timer.isValid) {

    [timer invalidate];

}

timer=nil;

[self dismissViewControllerAnimated:YES completion:nil];

}

在官方文档中我们可以看到 [timer invalidate]是唯一的方法将定时器从循环池中移除。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,087评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,932评论 25 709
  • 想写很多东西,因为懒没有写成,希望能在简书坚持下去。照片都删了就剩这一张。基础写作老师说要多练笔。感觉现在的大学生...
    崔尕傲阅读 259评论 0 2
  • 今天晨读分享的书是《TED演讲的秘密——18分钟改变世界》。为我们分享了演讲的三个技巧:主题唯一、故事为王、技巧先...
    饼姑娘阅读 275评论 0 1
  • 互联网把用户连在了一起,通过网络想要获取北京用户还是上海用户已经没有什么成本差别,正向马云所说的,前三十年是技术为...
    来过吧阅读 176评论 0 0