UILongPressGestureRecognizer

UILongPressGestureRecognizer

我在屏幕中间有一个节点,当前有此代码可将节点在屏幕左侧的每次轻击向左移动,在屏幕右侧的每次轻击向右移动。我想将其更改为长按而不是点击。我该怎么做?谢谢!这是水龙头的代码。

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    var touch: UITouch = touches.anyObject() as UITouch
    var location = touch.locationInNode(self)
    var node = self.nodeAtPoint(location)

    var speedOfTouch:CGFloat = 30


    //moves to the left and right by touch and plays sound effect by touch.
    for touch: AnyObject in touches {

        let location = touch.locationInNode(self)
        if location.x < CGRectGetMidX(self.frame) {
            hero.position.x -= speedOfTouch
            AudioPlayer.play()
        } else {
            hero.position.x += speedOfTouch
            AudioPlayer.play()
        }

最佳答案

这应该非常简单。在视图的初始化程序中初始化UILongPressGestureRecognizer。如果需要,可以选择在将手势识别器添加到视图之前调整minimumPressDurationallowableMovement属性。

override init(frame aRect: CGRect) {
    // ...

    // Create the long press gesture recognizer, and configure it
    // to call a method named viewLongPressed: on self
    let longPress = UILongPressGestureRecognizer(target: self, action: "viewLongPressed:")

    // Optionally configure it - see the documentation for explanations
    // longPress.minimumPressDuration = 1.0
    // longPress.allowableMovement = 15

    // Add the gesture recognizer to this view (self)
    addGestureRecognizer(longPress)

    // ...
}


然后只需实现回调方法:

func viewLongPressed(gestureRecognizer: UIGestureRecognizer) {
    // Handle the long press event by examining the passed in
    // gesture recognizer
}

关于xcode - 如何在Swift Spritekit的应用程序中使用UILongPressGestureRecognizer?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29049447/

10-11 07:30