首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >touchesBegan诉UITapGestureRecognizer

touchesBegan诉UITapGestureRecognizer
EN

Stack Overflow用户
提问于 2015-12-17 16:54:05
回答 1查看 4.4K关注 0票数 4

所以我正在用斯威夫特做一个斯普林特套件游戏。我想知道哪种手势是明智的呢?

目前,我有一个处理所有点击的override func touchesBegan和一个处理长点击/搁置的UILongPressGestureRecognizer

所以你知道,按下按钮跳下英雄。长时间的守住使英雄躲开。

由于某些原因,我的longPress函数并不总是被调用(有时您可以按10次并保持10次,然后它停止被调用(不被识别),另一些时候它是5,它是变化的),这导致昨天一整天都在尝试新的东西并进行调查,这使我想到了这个问题。

最好是使用touchesBegan,还是将所有的触摸调用移动到由UITapGestureRecognizer处理的新函数中?

我确实把所有的东西都从touchesBegan转移到了UITapGestureRecognizer,但是它看起来非常缓慢。但我可能搞错了?

recognisers是这样设置的:

代码语言:javascript
复制
func setupRecognizers() {
    let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
    view!.addGestureRecognizer(tapRecognizer)

    let longTapRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("handleLongPress:"))
    longTapRecognizer.minimumPressDuration = 0.2
    view!.addGestureRecognizer(longTapRecognizer)

}

这些是处理手势的功能:

代码语言:javascript
复制
func handleTap(recognizer: UIGestureRecognizer) {
    //currently all handled in touchesBegan
}

func handleLongPress(recognizer: UIGestureRecognizer) {
    print("1 --------> longPress Called.... ", recognizer.state.rawValue, gameState)
    if gameState == .Play {
       //do stuff
       //duck Hero
    } else {
       //switch gameState
    }
}

这是处理触摸/水龙头的函数:

代码语言:javascript
复制
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

for touch in touches {
     let location = touch.locationInNode(self)

     //do stuff

     switch gameState {
        case .MainMenu:
        break
        ... //more states
        }
   }
   super.touchesBegan(touches, withEvent: event)
}

如果我将所有东西从touchesBegans移动到tapRecogniser (上面的空函数),我也必须实现这一点,以转换触摸位置坐标:

代码语言:javascript
复制
func handleTap(recognizer: UIGestureRecognizer) {
    let location = convertPointFromView(CGPoint(x: recognizer.locationInView(nil).x, y: recognizer.locationInView(nil).y))
    print("Converted Coords: ", location)

    //then do all touchesBegan stuff
 }

这两种我都试过了,但后者看起来真的很慢,而且行动迟缓。也许我忘了去实现一些推荐的东西?

似乎我的longPress手势并不总是被调用,这两者之间可能有冲突吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-17 20:58:10

所以,如果你在红色的广场上坚持两秒钟,你就会得到一条信息,当你放手的时候,信息就消失了。您可能需要添加一些布尔在那里,以确保您的角色没有重复一些行动,每帧后,2秒按钮举行。这应该足以让你满怀希望地开始工作

代码语言:javascript
复制
import SpriteKit

class GameScene: SKScene {

    // time values
    var delta = NSTimeInterval(0)
    var last_update_time = NSTimeInterval(0)

    var longTouchTimer = NSTimeInterval(0)
    var touched = false
    var testLabel = SKLabelNode(text: "you have touched for awhile now bro")

    let touchSprite = SKSpriteNode(color: SKColor.redColor(), size: CGSizeMake(100, 100))

    override func didMoveToView(view: SKView) {
        touchSprite.position = CGPointMake(self.size.width/2, self.size.height/2)
        addChild(touchSprite)

        testLabel.hidden = true
        testLabel.position = touchSprite.position
        testLabel.position.y += 100
        testLabel.fontSize = 20
        addChild(testLabel)
    }


    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)

            if touchSprite.containsPoint(location) {
                touched = true
            }
        }
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)

            if !touchSprite.containsPoint(location) {
                touched = false
                longTouchTimer = 0
            }
        }
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        touched = false
        longTouchTimer = 0
    }

    override func update(currentTime: NSTimeInterval) {
        if last_update_time == 0.0 {
            delta = 0
        } else {
            delta = currentTime - last_update_time
        }

        last_update_time = currentTime

        if touched {
            longTouchTimer += delta
        }

        if longTouchTimer >= 2 {
            testLabel.hidden = false
        } else {
            testLabel.hidden = true
        }
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34340113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档