Ios 如何用“停止拍摄”;“NSTimer”;

Ios 如何用“停止拍摄”;“NSTimer”;,ios,swift,nstimer,Ios,Swift,Nstimer,我有一个角色拍摄,它总是在你触摸屏幕时打开,但我希望它在你停止触摸屏幕时停止。现在,当你触摸屏幕时,它将开始拍摄,但当你停止触摸屏幕时,它不会停止 override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { /* Called when a touch begins */ for touch: AnyObject in touches { let loca

我有一个角色拍摄,它总是在你触摸屏幕时打开,但我希望它在你停止触摸屏幕时停止。现在,当你触摸屏幕时,它将开始拍摄,但当你停止触摸屏幕时,它不会停止

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        player.position.x = location.x
        var timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("spawnShot"), userInfo: nil, repeats: true)
    }
}

override func touchesEnded(touches: Set<NSObject>, withEvent: UIEvent){
    for touch: AnyObject in touches{
        var timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("spawnShot"), userInfo: nil, repeats: false)
    }
}
override func touchsbegined(touchs:Set,withEvent-event:UIEvent){
/*当触摸开始时调用*/
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
player.position.x=location.x
var timer=NSTimer.scheduledTimerWithTimeInterval(0.5,目标:self,选择器:选择器(“spawnShot”),userInfo:nil,repeats:true)
}
}
覆盖函数touchesend(touchs:Set,withEvent:UIEvent){
用于触摸:触摸中的任何对象{
var timer=NSTimer.scheduledTimerWithTimeInterval(0.5,目标:self,选择器:选择器(“spawnShot”),userInfo:nil,repeats:false)
}
}

要停止重复计时器,您需要使其无效,为此,请将
计时器
保留为属性,然后将其安排在
touchsbegind
中,当您要停止计时器时,请使用其
touchseded
方法使其无效,您可以像这样
使计时器无效

if self.timer.valid
        {
            self.timer.invalidate()
            self.timer = nil
        }

touchesend
中,您需要使在
touchesbeated
中创建的计时器无效。它只是给我一个错误,说“timer”不是成员,因为您没有将其作为成员变量。。。然后你可以用self访问它,将它添加为属性