UILongPressGestureRecognizer无法正常工作

时间:2015-07-17 18:51:30

标签: swift sprite-kit uigesturerecognizer

我想在屏幕右侧使节点跳跃,左半部分的前半部分向左移动,后半部分向右移动。

有些东西不行:

  • 当我快速左,右,左,右快速点击时(它将停止并且不会移动节点)

  • 当我点击屏幕右侧时点击左侧或右侧部分并不是每次节点移动时都会下降,有时候第一个问题会重复

以下是具有相同机制的游戏示例:

https://itunes.apple.com/us/app/staying-together/id923670329?mt=8

override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    let tapper = UILongPressGestureRecognizer(target: self, action: "tappedScreen:");
    tapper.minimumPressDuration = 0.1
    view.addGestureRecognizer(tapper)
}

func jumpAction(){
    if(!isJumping){
        hero.physicsBody?.applyImpulse(CGVector(dx: 0, dy: ySpeed))
    }   
    isJumping = true
}

func tappedScreen(recognizer: UITapGestureRecognizer){
    if(recognizer.state == UIGestureRecognizerState.Began){     
        let moveLeft = SKAction.moveByX(-15, y: 0, duration: 0.1)
        let moveRight = SKAction.moveByX(15, y: 0, duration: 0.1)

        let touchY = self.convertPointFromView(recognizer.locationInView(self.view)).y

        // only the bottom part of the screen, that way the UI button will be able to touch
        if(touchY < self.frame.size.height/4){      
            if(touchX > 0){
                println("up - longer");
            } else if(touchX < -(self.frame.size.width/4)){      
                println("left - longer");
                hero.runAction(SKAction.repeatActionForever(moveLeft), withKey: "longTap")
            } else {
                println("right - longer");
                hero.runAction(SKAction.repeatActionForever(moveRight), withKey: "longTap")
            }
        } 
    } else {
        if(touchX <= 0){
            if (recognizer.state == UIGestureRecognizerState.Ended) {
                println("ended, also stops the node from moving");
                hero.removeActionForKey("longTap")
            }
        }
    }
}

// I think this is need to mmove the node on single tap
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    let touch: UITouch = touches.first as! UITouch;
    let location:CGPoint = touch.locationInNode(self);

    let moveLeft = SKAction.moveByX(-15, y: 0, duration: 0.1)
    let moveRight = SKAction.moveByX(15, y: 0, duration: 0.1)

    // only the bottom part of the screen, that way the UI button will be able to touch
    if(location.y < self.frame.size.height/4){
        if(location.x > 0){
            println("up");
            self.jumpAction();
        } else if(location.x < -(self.frame.size.width/4)){
            hero.runAction(moveLeft);
            println("left");
        } else {
            hero.runAction(moveRight);
            println("right");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

因为tappedScreen有一个引用UITapGestureRecognizer的参数。你必须将它设置为UILongPressGestureRecognizer。

func tappedScreen(识别器:UILongPressGestureRecognizer){

}

希望有所帮助:)