6.15 Creating Timers

本文介绍了如何使用Objective-C中的NSTimer实现循环执行特定任务的方法。通过实例展示了定时器的基本用法,包括启动、停止定时器及不同创建方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


定时器,循环执行某一任务

看下书上的例子


- (void) paint:(NSTimer *)paramTimer{

    /* Do something here */

    NSLog(@"Painting");

}

- (void) startPainting{

    self.paintingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0

                                                          target:self

                                                        selector:@selector(paint:)

                                                        userInfo:nil

                                                         repeats:YES];

}

- (void) stopPainting{

    if (self.paintingTimer != nil){

        [self.paintingTimer invalidate];

    }

}


- (void)applicationWillResignActive:(UIApplication *)application

{

    [self stopPainting];

}


- (void)applicationDidBecomeActive:(UIApplication *)application

{

    [self startPainting];

}


输出:

2014-03-12 16:30:58.362 cookbook[710:a0b] Painting

2014-03-12 16:30:59.362 cookbook[710:a0b] Painting

2014-03-12 16:31:00.362 cookbook[710:a0b] Painting

2014-03-12 16:31:01.362 cookbook[710:a0b] Painting

。。。。。。


[self.paintingTimer invalidate]; invalidate方法会释放掉定时器,不需要再手动释放。它不仅会释放定时器本身,还会释放它retain的 userInfo参数对象。

如果repeats值是NO。则定时器在触发后会自己invalidate


还可以用NSInvocation来创建定时器


- (void) startPainting{

    /* Here is the selector that we want to call */

    SEL selectorToCall = @selector(paint:);

    /* Here we compose a method signature out of the selector.

     We know that the selector is in the current class so it is easy to construct the method signature */

    NSMethodSignature *methodSignature =[[self class] instanceMethodSignatureForSelector:selectorToCall];

    /* Now base the invocation on the method signature. We need this invocation to schedule a timer */

    NSInvocation *invocation =[NSInvocation invocationWithMethodSignature:methodSignature];

    [invocation setTarget:self];

    [invocation setSelector:selectorToCall];

    /* Start a scheduled timer now */

    self.paintingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0

                                                      invocation:invocation

                                                         repeats:YES];

}



还可以先创建,需要的时候在手动加到Run loop里面去


- (void) startPainting{

    self.paintingTimer = [NSTimer timerWithTimeInterval:1.0 target:self

                                               selector:@selector(paint:)

                                               userInfo:nil

                                                repeats:YES];

    /* Do your processing here and, whenever you are ready,

     use the addTimer:forMode instance method of the NSRunLoop class in order to schedule the timer on that run loop */

    [[NSRunLoop currentRunLoop] addTimer:self.paintingTimer forMode:NSDefaultRunLoopMode];

}

   

当然了,也可以NSInvocation加手动


- (void) startPainting{

    /* Here is the selector that we want to call */

    SEL selectorToCall = @selector(paint:);

    /* Here we compose a method signature out of the selector. We

     know that the selector is in the current class so it is easy

     to construct the method signature */

    NSMethodSignature *methodSignature =[[self class] instanceMethodSignatureForSelector:selectorToCall];

    /* Now base the invocation on the method signature. We need this invocation to schedule a timer */

    NSInvocation *invocation =[NSInvocation invocationWithMethodSignature:methodSignature];

    [invocation setTarget:self];

    [invocation setSelector:selectorToCall];

    self.paintingTimer = [NSTimer timerWithTimeInterval:1.0

                                             invocation:invocation

                                                repeats:YES];;

    /* Do your processing here and, whenever you are ready,

     use the addTimer:forMode instance method of the NSRunLoop class in order to schedule the timer on that run loop */

    [[NSRunLoop currentRunLoop] addTimer:self.paintingTimer forMode:NSDefaultRunLoopMode];

}
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值