【问题标题】:Objective c - NSTimer, CADisplayLink, Thread目标 c - NSTimer、CADisplayLink、线程
【发布时间】:2012-06-19 09:39:07
【问题描述】:

我目前正在制作一个带有倒数计时器的小游戏。我使用 NSTimer,你可以得到额外的时间或更少的时间,取决于你的行动。当我向计时器添加或删除秒数时,时间标签有问题。问题出在我身上,我正在调用我的方法 timerAction,当我添加或删除时间时,他在标签中设置了新时间,然后 NSTimer 也在调用该方法。如果我不调用 timerAction,我必须等待 1 秒才能用 NSTimer 刷新标签。

那么,我应该怎么做才能解决我的问题呢?

感谢您的帮助。

【问题讨论】:

    标签: objective-c nstimer countdown


    【解决方案1】:

    首先,我想问一下您的代码。

    此外,在这种情况下,我会喜欢这种模式。

    @property (nonatomic) NSInteger remainingTime;
    @property (nonatomic) NSTimer *timer;
    
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES]; 
    
    - (void)timerAction {
        if (!countingFinish) {
           remainingTime --;
           //backend regular action
           [self frontEndUpdate];
        }else {
           [timer invalidate];
        }
    }
    
    - (bool)countingFinish {
        return (reamainingTime <= 0);
    }
    
    - (void)frontEndUpdate {
        //update time label, or other front end update action
    }
    
    - (void)addCountingRemaining:(NSInteger) _sec {
         remainingTime += _sec;
         [self frontEndUpdate];
    }
    

    顺便说一句,这是 juz 草案代码。希望对你有帮助

    【讨论】:

    • 谢谢您的代码帮助。事实上,我在 timerAction 函数中做了坏事。我在这里设置我的标签并执行“秒--”所以当我调用 timerAction 来设置我的标签时,我也设置了秒--并且计时器也调用 timerAction,所以我要花 2 秒而不是 1 秒。再次感谢;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    相关资源
    最近更新 更多