我有一个UIPanGestureRecognizer
,当手势到达左侧或右侧屏幕边缘时,我想调用一种方法。
我想到了获取位置并将其与边缘的位置进行比较。
有更好的方法吗?如果没有,而不是选择自己边缘开始的坐标,标准是什么?是10%吗?更多?少?
答案 0 :(得分:1)
在检测用户是否接近屏幕的左/右边缘时似乎有效。
override func viewDidLoad() {
super.viewDidLoad()
let screenWidth = UIScreen.mainScreen().bounds.size.width
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first as UITouch!{
let currentPoint = floor(touch.locationInView(self.view).x)
//print("touched point \(currentPoint) x")
edgeReach(currentPoint)
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first as UITouch! {
let currentPoint = floor(touch.locationInView(self.view).x)
//print("moved to point \(currentPoint) x")
edgeReach(currentPoint)
}
}
func edgeReach(locationX: CGFloat) {
if locationX >= 364 {
print("closer to right edge")
}
if locationX <= 50{
print("close to left edge")
}
}
如果您将其合并到当前的UIPanGestureRecognizer中。