【问题标题】:signal sigbart error when using NSTimer within a touches began function在 touchesbegan 函数中使用 NSTimer 时发出信号 sigabrt 错误
【发布时间】:2016-03-26 05:41:25
【问题描述】:

我正在尝试构建一个应用程序,当第一次触摸屏幕时,它会启动一个 NSTimer,但是在触摸开始功能中使用 NSTimer 我收到信号 sigbart 错误。如果有人有任何替代方法,我可以使用它来获得相同的结果,我将不胜感激。

这是我当前的基本代码

    var condition = 0

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if(condition == 1) {
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timerUpdate"), userInfo: nil, repeats: true)
    }

    cat.physicsBody?.velocity = CGVectorMake(0, 0)
    cat.physicsBody?.applyImpulse(CGVectorMake(0, 1))


}

【问题讨论】:

    标签: ios swift nstimer touchesbegan


    【解决方案1】:

    您必须为选择器添加函数timerUpdate

     override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if(condition == 1) {
            timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timerUpdate"), userInfo: nil, repeats: true)
        }
    
        cat.physicsBody?.velocity = CGVectorMake(0, 0)
        cat.physicsBody?.applyImpulse(CGVectorMake(0, 1))
    }
    
    func timerUpdate(){
        print("timerUpdate")
    }
    

    【讨论】:

    • LOL 非常感谢,我觉得我不这样做有点愚蠢,但哦,好吧。无论如何,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    相关资源
    最近更新 更多