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;

    }

  • 相关阅读:
    模拟赛20181101 雅礼 Wearry 施工 蔬菜 联盟
    模拟赛20181031 雅礼 Wearry 养花 折射 画作
    set/priority_queue的运算符重载
    set的完整用法
    最长公共上升子序列 O(n^2)
    无向图边双联通分量 tarjan 模板
    ID 迭代加深搜索 模板 埃及分数
    树上背包DP Luogu P2014 选课
    A* 第k短路
    [POJ3468]关于整数的简单题 (你想要的)树状数组区间修改区间查询
  • 原文地址:https://www.cnblogs.com/tang910103/p/5061258.html
Copyright © 2011-2022 走看看