zoukankan      html  css  js  c++  java
  • 关于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;

    }

  • 相关阅读:
    最短路径之spfa
    最短路径之Bellman-Ford——解决负权边
    最短路径之Floyd-Warshall算法
    图上最短路径问题
    它们其实都是图(二分图)
    记忆化结果再利用 进一步探讨递推关系
    leetcode 376. 摆动序列 java
    leetcode 368. 最大整除子集 java
    leetcode 96. 不同的二叉搜索树 java
    leetcode 454. 四数相加 II java
  • 原文地址:https://www.cnblogs.com/tang910103/p/5061258.html
Copyright © 2011-2022 走看看