正如标题所说,我该如何做到这一点?我可以获得第一个触控,但我不知道如果是滑动手势,如何获得最后一个触控。使用以下代码,我得到错误:类型'Set'的值没有成员'last'。我不太确定当触摸被释放时如何获得位置。
override func touchesMoved(_ touches: Set<UITouch>, with: UIEvent?){
let firstTouch:UITouch = touches.first! as UITouch
let lastTouch:UITouch = touches.last! as UITouch
let firstTouchLocation = firstTouch.location(in: self)
let lastTouchLocation = lastTouch.location(in: self)
}
我想要得到第一个点和第二个点的位置。然后,我想使用滑动的持续时间,线的大小和线的角度来创建一个用于SKNode的脉冲矢量。是否有任何方法可以使用UITouch提取这些信息?非常感谢任何帮助。谢谢你的时间!
我在屏幕上也有一些基本的触摸来运行函数
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch:UITouch = touches.first! as UITouch
let touchLocation = touch.location(in: self)
if playButton.contains(touchLocation) && proceed == true{
playSound1()
Ball.physicsBody?.affectedByGravity = true
proceed = false}
else if infoButton.contains(touchLocation) && proceed == true{
playSound1()
loadScene2()}
else {}
}
你必须在touchesbegan中存储值并与touchesended中的值进行比较,然后在那里进行计算。请注意,触摸可以被电话之类的东西取消,所以你也需要预料到这一点。
class v: UIViewController {
var startPoint: CGPoint?
var touchTime = NSDate()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard touches.count == 1 else {
return
}
if let touch = touches.first {
startPoint = touch.location(in: view)
touchTime = NSDate()
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
defer {
startPoint = nil
}
guard touches.count == 1, let startPoint = startPoint else {
return
}
if let touch = touches.first {
let endPoint = touch.location(in: view)
//Calculate your vector from the delta and what not here
let direction = CGVector(dx: endPoint.x - startPoint.x, dy: endPoint.y - startPoint.y)
let elapsedTime = touchTime.timeIntervalSinceNow
let angle = atan2f(Float(direction.dy), Float(direction.dx))
}
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
startPoint = nil
}
}