【问题标题】:UILongPressGestureRecognizer doesn't work properlyUILongPressGestureRecognizer 无法正常工作
【发布时间】:2015-07-17 18:51:30
【问题描述】:

我想让屏幕右侧让节点跳转,左边的前半部分向左,后半部分向右移动。

一些不好的东西:

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

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

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

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");
        }
    }
}

【问题讨论】:

    标签: swift sprite-kit uigesturerecognizer


    【解决方案1】:

    这是因为 tappedScreen 有一个引用 UITapGestureRecognizer 的参数。您必须将其设置为 UILongPressGestureRecognizer。

    func tappedScreen(识别器:UILongPressGestureRecognizer){

    }

    希望对你有帮助:)

    【讨论】:

    • 我做了你建议的改变,但问题仍然存在
    • 你说 UILongPressGestureRecognizer 不能正常工作。
    • 你为什么还要使用 uilongpress 来敲击呢?只需使用水龙头。 0.1 的东西不好,因为点击时间短于 0.1 秒
    • 是的,如果玩游戏,英雄会在点击时移动,长按也会移动!另一个游戏是 ketchapp 的 Run Bird Run
    • 现在我按下向右移动,然后我快速释放并按下向左移动,但它冻结了,最后一个日志是“结束,也停止节点移动”UIGestureRecognizerState.Ended。不触发向左移动部分
    猜你喜欢
    • 2016-12-01
    • 1970-01-01
    • 2016-09-01
    • 2012-07-11
    • 2018-04-08
    • 2017-04-20
    • 2018-10-02
    • 2016-09-04
    • 2010-10-06
    相关资源
    最近更新 更多