Rea*_*ion 5 touchesbegan uigesturerecognizer ios sprite-kit swift
所以我正在使用 swift 制作 spritekit 游戏。我想知道哪种手势最适合使用?
目前我有一个override func touchesBegan处理所有点击和一个UILongPressGestureRecognizer处理长点击/按住。
所以你知道,轻按一下按钮并跳跃英雄。长时间的坚持会让英雄闪避。
由于某种原因,我的 longPress 函数并不总是被调用(有时你可以按住 10 次,然后它就停止被调用(无法识别),其他时候它是 5 次,它会有所不同),这导致了昨天一整天尝试新事物并进行调查,这让我想到了这个问题。
使用touchesBegan或将所有触摸调用移至由 处理的新函数是否更好UITapGestureRecognizer?
我确实把所有东西都从 移到touchesBegan了UITapGestureRecognizer,但看起来很慢。但我可能执行错了?
这是如何recognisers设置的:
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)
}
Run Code Online (Sandbox Code Playgroud)
这些是处理手势的函数:
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
}
}
Run Code Online (Sandbox Code Playgroud)
这是处理触摸/点击的函数:
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)
}
Run Code Online (Sandbox Code Playgroud)
如果我将所有内容从touchesBegansa tapRecogniser(上面的空函数)移动,我也必须实现它,以转换触摸位置坐标:
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
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这两种方法,但后者似乎非常缓慢且迟缓。也许我忘记实施推荐的东西?
似乎我的长按手势并不总是被调用,这之间可能存在一些冲突吗?
因此,如果您按住红色方块两秒钟,您会收到一条消息,当您松开时,该消息就会消失。您可能需要在其中添加一些布尔值,以确保您的角色在按住 2 秒按钮后不会每帧都重复某些动作。这应该足以让你开始希望
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4667 次 |
| 最近记录: |