gpt4 book ai didi

swift - UILongPressGestureRecognizer 无法正常工作

转载 作者:行者123 更新时间:2023-11-28 09:03:27 27 4
gpt4 key购买 nike

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

一些不合适的东西:

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

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

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

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

最佳答案

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

func tappedScreen(识别器:UILongPressGestureRecognizer){

希望对您有所帮助:)

关于swift - UILongPressGestureRecognizer 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31482752/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com