Swift UILongPressGestureRecognitor不';我不能正常工作

Swift UILongPressGestureRecognitor不';我不能正常工作,swift,sprite-kit,uigesturerecognizer,Swift,Sprite Kit,Uigesturerecognizer,我想让屏幕右侧的节点跳跃,左上半部分向左,下半部分向右移动 一些不好的东西: 当我多次快速点击左、右、左、右(它将停止并且不会移动节点) 当我点击屏幕的右侧部分并点击左侧或右侧部分时,并非每次节点下降时都会移动,有时第一个问题会重复 下面是一个具有相同机制的游戏示例: 覆盖func didMoveToView(视图:SKView){ /*在这里设置场景*/ 让tapper=ui长按手势识别器(目标:self,动作:“tappedScreen:”); tapper.minimumPressD

我想让屏幕右侧的节点跳跃,左上半部分向左,下半部分向右移动

一些不好的东西:

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

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

下面是一个具有相同机制的游戏示例:

覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
让tapper=ui长按手势识别器(目标:self,动作:“tappedScreen:”);
tapper.minimumPressDuration=0.1
view.AddGestureRecognitor(tapper)
}
func jumpAction(){
如果(!正在跳跃){
hero.physicsBody?.applyImpulse(CGVector(dx:0,dy:ySpeed))
}   
是真的吗
}
func tappedScreen(识别器:UITappedRecognitor){
如果(recognizer.state==UIGestureRecognizerState.start){
让moveLeft=SKAction.moveByX(-15,y:0,持续时间:0.1)
让moveRight=SKAction.moveByX(15,y:0,持续时间:0.1)
让touchY=self.convertPointFromView(recognizer.locationInView(self.view)).y
//只有屏幕的底部,这样UI按钮才能触摸
如果(触觉<自身框架尺寸高度/4){
如果(触摸X>0){
println(“向上-更长”);
}如果(touchX<-(self.frame.size.width/4)){
println(“左-长”);
英雄.runAction(SKAction.repeatActionForever(左移),键为:“longTap”)
}否则{
println(“右-长”);
英雄.runAction(SKAction.repeatActionForever(moveRight),键为:“longTap”)
}
} 
}否则{
如果(触摸X 0){
println(“up”);
自我作用();
}否则,如果(位置x<-(自身框架尺寸宽度/4)){
英雄。奔跑动作(左移);
println(“左”);
}否则{
英雄。奔跑动作(右移);
println(“权利”);
}
}
}

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

func tappedScreen(识别器:UILongPressGestureRecognitor){

}


希望有帮助:)

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

func tappedScreen(识别器:UILongPressGestureRecognitor){

}


希望有帮助:)

我做了您建议的更改,但问题仍然存在。您说uilongpress GestureRecognitor无法正常工作。您为什么还要使用uilongpress进行点击?用水龙头就行了。0.1秒不好,因为点击时间短于0.1秒,如果玩游戏,英雄可以点击移动,也可以长按!另一个游戏是运行Ketchapright运行的Bird现在我按下向右移动,然后快速释放并按下向左移动,但它冻结,最后一个日志是“结束,也停止节点移动”UIgestureRecognitizerState.ended。“向左移动”部分未触发。我进行了您建议的更改,但问题仍然存在。您说uilongpress手势识别器工作不正常。您为什么还要使用uilongpress进行点击?用水龙头就行了。0.1秒不好,因为点击时间短于0.1秒,如果玩游戏,英雄可以点击移动,也可以长按!另一个游戏是运行Ketchapright运行的Bird现在我按下向右移动,然后快速释放并按下向左移动,但它冻结,最后一个日志是“结束,也停止节点移动”UIgestureRecognitizerState.ended。未触发“向左移动”部分
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");
        }
    }
}