gpt4 book ai didi

swift - touchesEnded 未被识别

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:14 30 4
gpt4 key购买 nike

当我调用 touchesBegan()

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("TOUCH_BEGAN")
}

我得到打印语句。我注意到,如果我不移动接触点,

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

print("TOUCH_ENDED")
}

没有被调用,但是当我移动接触点时被调用。调用 touchesEnded() 是否需要一些最小的移动?如果是这样,是否有一种方法可以覆盖它,以确保始终调用 touchesEnded()


完整的代码太长了,下面再补充一点:

import SpriteKit
import UIKit
import Foundation

class GameScene: SKScene, SKPhysicsContactDelegate {
// MARK: Properties
var touchStart: CGPoint?
let minSwipeDistance:CGFloat = 22
var distance: CGFloat = 0

override func didMoveToView(view: SKView) {
super.didMoveToView(view)
//...

// MARK: INCLUDE SWIPE GESTURES

let swipeRight: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:"))
swipeRight.direction = .Right
swipeRight.cancelsTouchesInView = false
swipeRight.delaysTouchesEnded = false
view.addGestureRecognizer(swipeRight)

let swipeLeft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedLeft:"))
swipeLeft.direction = .Left
swipeLeft.cancelsTouchesInView = false
swipeLeft.delaysTouchesEnded = false
view.addGestureRecognizer(swipeLeft)

let swipeUp: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedUp:"))
swipeUp.direction = .Up
swipeUp.cancelsTouchesInView = false
swipeUp.delaysTouchesEnded = false
view.addGestureRecognizer(swipeUp)

let swipeDown: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedDown:"))
swipeDown.direction = .Down
swipeDown.cancelsTouchesInView = false
swipeDown.delaysTouchesEnded = false
view.addGestureRecognizer(swipeDown)
}

// MARK: ******* User Interaction ******

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
touchStart = touch.locationInNode(self)
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)
}
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
}

override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first, start = touchStart {
let location = touch.locationInNode(self)
// Compute the distance between the two touches
let dx = location.x - start.x
let dy = location.y - start.y
distance = sqrt(dx*dx + dy*dy)
if distance < minSwipeDistance {
let node = nodeAtPoint(location)
print("NODE NAME: \(node.name)")
}
}
// Reset the initial location
touchStart = nil
}

// MARK: handle swipes
func swipedRight(sender: UISwipeGestureRecognizer) {
print("swiped right")
}

func swipedLeft(sender: UISwipeGestureRecognizer) {
print("swiped left")
}

func swipedUp(sender: UISwipeGestureRecognizer) {
print("swiped up")
}

func swipedDown(sender: UISwipeGestureRecognizer) {
print("swiped down")
}
}

最佳答案

您不能确保 touchesEnded 被调用,因为 touchesCancelled 可能被调用。您应该始终在两者中实现结束功能。

关于swift - touchesEnded 未被识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35365613/

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