2016-02-12 67 views
4

當我打電話touchesBegan()touchesEnded不被認可

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

我得到的打印語句。 我注意到的是,如果我不動的接觸點,

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

     print("TOUCH_ENDED") 
} 

不會被調用,但是當我移動的接觸點。需要撥打touchesEnded()需要一些最低限度的運動嗎?如果是這樣,是否有覆蓋這個方法,以確保touchesEnded()總是被稱爲?


完整的代碼太長增加,但略偏下方:

import SpriteKit 
import UIKit 
import Foundation 

class GameScene: SKScene, SKPhysicsContactDelegate { 
    // MARK: Properties 
    var touchStart: CGPoint? 
    let minSwipeDistance:CGFloat = 22 
    var distance: CGFloat = 0 

    override func didMoveToView(view: SKView) { 
     super.didMoveToView(view) 
     //... 

     // MARK: INCLUDE SWIPE GESTURES 

     let swipeRight: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:")) 
     swipeRight.direction = .Right 
     swipeRight.cancelsTouchesInView = false 
     swipeRight.delaysTouchesEnded = false 
     view.addGestureRecognizer(swipeRight) 

     let swipeLeft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedLeft:")) 
     swipeLeft.direction = .Left 
     swipeLeft.cancelsTouchesInView = false 
     swipeLeft.delaysTouchesEnded = false 
     view.addGestureRecognizer(swipeLeft) 

     let swipeUp: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedUp:")) 
     swipeUp.direction = .Up 
     swipeUp.cancelsTouchesInView = false 
     swipeUp.delaysTouchesEnded = false 
     view.addGestureRecognizer(swipeUp) 

     let swipeDown: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedDown:")) 
     swipeDown.direction = .Down 
     swipeDown.cancelsTouchesInView = false 
     swipeDown.delaysTouchesEnded = false 
     view.addGestureRecognizer(swipeDown) 
    } 

    // MARK: ******* User Interaction ****** 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     if let touch = touches.first { 
      touchStart = touch.locationInNode(self) 
      let location = touch.locationInNode(self) 
      let node = nodeAtPoint(location) 
     } 
    } 

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

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

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     if let touch = touches.first, start = touchStart { 
      let location = touch.locationInNode(self) 
      // Compute the distance between the two touches 
      let dx = location.x - start.x 
      let dy = location.y - start.y 
      distance = sqrt(dx*dx + dy*dy) 
      if distance < minSwipeDistance { 
       let node = nodeAtPoint(location) 
       print("NODE NAME: \(node.name)") 
      } 
     } 
     // Reset the initial location 
     touchStart = nil 
    } 

    // MARK: handle swipes 
    func swipedRight(sender: UISwipeGestureRecognizer) { 
     print("swiped right") 
    } 

    func swipedLeft(sender: UISwipeGestureRecognizer) { 
     print("swiped left") 
    } 

    func swipedUp(sender: UISwipeGestureRecognizer) { 
     print("swiped up") 
    } 

    func swipedDown(sender: UISwipeGestureRecognizer) { 
     print("swiped down") 
    } 
} 
+0

你不應該自己調用touchesBegan()。 – TechBee

+0

@TechBee我不明白你的意思? touchesBegan(),touchesMoved()和touchesEnded()等方法都可以稱爲 –

+0

這些是調用委託方法。將它們視爲回調方法。你想不要打電話給他們。 – TechBee

回答

6

不能保證touchesEnded被調用,因爲touchesCancelled可能會改爲調用。您應始終在兩者中實現結束功能。

+0

謝謝。你能舉一個例子說明你的意思是「結束功能嗎?」 –

+0

不要緊,我得到它...只需將相同的代碼複製到touchesCancelled()和touchesEnded()。這是一個壯觀的提示。 –