如何使用计时器切换按钮的可见性?

时间:2014-08-01 03:33:00

标签: ios nstimer

我添加了一个NSTimer,通过将操作设置为隐藏NO

来显示我的费率按钮
NSMethodSignature *sgn = [self methodSignatureForSelector:@selector(onTick:)];
    NSInvocation *inv = [NSInvocation invocationWithMethodSignature: sgn];
    [inv setTarget: self];
    [inv setSelector:@selector(onTick:)];

    NSTimer *t = [NSTimer timerWithTimeInterval: 10.0
                                     invocation:inv
                                        repeats:YES];

    NSRunLoop *runner = [NSRunLoop currentRunLoop];
    [runner addTimer: t forMode: NSDefaultRunLoopMode];

然后

-(void)onTick:(NSTimer *)timer {
_Rateapp1.hidden = NO;
_ratebomb.hidden = NO;
_ratelab1.hidden = NO;
}

但是如何让按钮重新显示为YES?

1 个答案:

答案 0 :(得分:0)

假设您的目标是在每次定时器关闭时切换按钮的显示,您只需执行以下操作:

- (void)onTick:(NSTimer *)timer {
    _Rateapp1.hidden = !_Rateapp1.hidden;
    _ratebomb.hidden = !_ratebomb.hidden;
    _ratelab1.hidden = !_ratelab1.hidden;
}

您可以用以下内容替换所有计时器创建代码:

NSTimer *t = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(onTick:) userInfo:nil repeats:YES];

无需调用或运行代码。我还建议您保留对计时器的引用,以便在需要时使计时器无效(停止)。