关于NSTimer的几种构建方式

 - (IBAction)startButtonAction1:(id)sender {

   // 使用scheduledTimer类方法构建出的Timer,会以NSDefaultRunLoopMode模式放入RunLoop中,也就是说计时器即刻开始工作了

    // 这是selector的用法

    if (!timer_) {

        timer_ = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];

    }

}

 

- (IBAction)startButtonAction2:(id)sender {

    // 使用scheduledTimer类方法构建出的Timer,会以NSDefaultRunLoopMode模式放入RunLoop中,也就是说计时器即刻开始工作了

    // 这是invocation的用法

    if (!timer_) {

        timer_ = [NSTimer scheduledTimerWithTimeInterval:1.0 invocation:[self timerActionInvocation] repeats:YES];

    }

}

 

- (IBAction)startButtonAction3:(id)sender {

    if (!timer_) {

        // 使用timer类方法构建出的Timer,不会放入RunLoop中,也就是说计时器在构建出来后并没有立即工作

        // 当将其放入RunLoop中后,计时器开始工作

        // 这是selector的用法

        timer_ = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];

        [[NSRunLoop mainRunLoop] addTimer:timer_ forMode:NSRunLoopCommonModes];

    }

}

 

- (IBAction)startButtonAction4:(id)sender {

    // 使用timer类方法构建出的Timer,不会放入RunLoop中,也就是说计时器在构建出来后并没有立即工作

    // 当将其放入RunLoop中后,计时器开始工作

    // 这是invocation的用法

    if (!timer_) {

        timer_ = [NSTimer timerWithTimeInterval:1.0 invocation:[self timerActionInvocation] repeats:YES];

        [[NSRunLoop mainRunLoop] addTimer:timer_ forMode:NSRunLoopCommonModes];

    }

}

 

- (IBAction)startButtonAction5:(id)sender {

    // 使用实例初始化方法构建出的Timer,不会放入RunLoop中,也就是说计时器在构建出来后并没有立即工作

    // 当将其放入RunLoop中后,计时器开始工作

    // 使用实例方法可以指定计时器开始工作的时间

    if (!timer_) {

        timer_ = [[[NSTimer alloc] initWithFireDate:[NSDate distantPast] interval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES] autorelease];

        [[NSRunLoop mainRunLoop] addTimer:timer_ forMode:NSRunLoopCommonModes];

    }

}

 

- (IBAction)stopButtonAction:(id)sender {

    // invalidate会停止计时器,并将其从RunLoop中移除

    if (timer_) {

        [timer_ invalidate];

        timer_ = nil;

    }

}

 

- (IBAction)pauseButtonAction:(id)sender {

    // 如果timer能够接收fire消息,则isValid属性值为YES,否则为NO

    // 将fireDate设置为未来,则表示timer不接受fire消息

    if (timer_ && timer_.isValid) {

        timer_.fireDate = [NSDate distantFuture];

    }

}

 

- (IBAction)continueButtonAction:(id)sender {

    // 如果timer能够接收fire消息,则isValid属性值为YES,否则为NO

    // 将fireDate设置为过去,则表示timer能接收fire消息

    if (timer_ && timer_.isValid) {

        timer_.fireDate = [NSDate distantPast];

    }

}

 

- (IBAction)fireButtonAction:(id)sender {

    // timer接收到fire消息则会触发一次对应的处理

    if (timer_ && timer_.isValid) {

        [timer_ fire];

    }

}

 

#pragma mark Misc

 

- (NSString *)currentTimeString {

    NSDate *date = [NSDate date];

    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];

    formatter.dateFormat = @"HH:mm:ss";

    

    return [formatter stringFromDate:date];

}

 

- (NSInvocation *)timerActionInvocation {

    SEL selector = @selector(timerAction:);

    NSMethodSignature *signature = [self methodSignatureForSelector:selector];

    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];

    invocation.target = self;

    invocation.selector = selector;

    

    return invocation;

}

posted on 2015-12-20 16:57  tang杰  阅读(203)  评论(0编辑  收藏  举报

导航