Swift UISwipeGestureRecognizer 如何检测对角线滑动?
Swift UISwipeGestureRecognizer how to detect diagonal swipes?
我需要检测所有向左滑动和向右滑动的滑动,包括向上滑动。我的意思是我需要检测 180 度区域中的所有滑动,我不确定我是否足够清楚。当我添加 .up .right 和 .left 时,它没有检测到对角线,例如左上,我该怎么办?谢谢!
UIPanGestureRecognizer 是实现此目的的方法:
private var panRec: UIPanGestureRecognizer!
private var lastSwipeBeginningPoint: CGPoint?
override func viewDidLoad() {
panRec = UIPanGestureRecognizer(target: self, action: #selector(ViewController.handlePan(recognizer:)))
self.view.addGestureRecognizer(panRec)
}
func handlePan(recognizer: UISwipeGestureRecognizer) {
if recognizer.state == .began {
lastSwipeBeginningPoint = recognizer.location(in: recognizer.view)
} else if recognizer.state == .ended {
guard let beginPoint = lastSwipeBeginningPoint else {
return
}
let endPoint = recognizer.location(in: recognizer.view)
// TODO: use the x and y coordinates of endPoint and beginPoint to determine which direction the swipe occurred.
}
}
我需要检测所有向左滑动和向右滑动的滑动,包括向上滑动。我的意思是我需要检测 180 度区域中的所有滑动,我不确定我是否足够清楚。当我添加 .up .right 和 .left 时,它没有检测到对角线,例如左上,我该怎么办?谢谢!
UIPanGestureRecognizer 是实现此目的的方法:
private var panRec: UIPanGestureRecognizer!
private var lastSwipeBeginningPoint: CGPoint?
override func viewDidLoad() {
panRec = UIPanGestureRecognizer(target: self, action: #selector(ViewController.handlePan(recognizer:)))
self.view.addGestureRecognizer(panRec)
}
func handlePan(recognizer: UISwipeGestureRecognizer) {
if recognizer.state == .began {
lastSwipeBeginningPoint = recognizer.location(in: recognizer.view)
} else if recognizer.state == .ended {
guard let beginPoint = lastSwipeBeginningPoint else {
return
}
let endPoint = recognizer.location(in: recognizer.view)
// TODO: use the x and y coordinates of endPoint and beginPoint to determine which direction the swipe occurred.
}
}