从 swift 3 中的触摸位置获取矢量
Getting a vector from a touch location in swift 3
正如标题所说,我该如何做到这一点?我可以得到第一次触摸,但如果是滑动手势,我不知道如何得到最后一次触摸。使用以下代码我得到错误:类型 '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 {}
}
您必须将值存储在 touches begin 中,并与 touches ended 中的值进行比较,然后在那里进行计算。请注意,可以通过 phone 调用之类的方式取消触摸,因此您也需要预料到这一点。
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
}
}
正如标题所说,我该如何做到这一点?我可以得到第一次触摸,但如果是滑动手势,我不知道如何得到最后一次触摸。使用以下代码我得到错误:类型 '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 {}
}
您必须将值存储在 touches begin 中,并与 touches ended 中的值进行比较,然后在那里进行计算。请注意,可以通过 phone 调用之类的方式取消触摸,因此您也需要预料到这一点。
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
}
}