2015-11-05 62 views
2

我有一个UIPanGestureRecognizer,我想在手势到达左侧或右侧屏幕边缘时调用方法。检测用户何时到达设备的左侧或右侧屏幕边缘

我想过如何获取位置并将其与边缘位置进行比较。

有没有更好的方法?如果没有,而不是选择自己的边缘开始坐标,什么是标准的?这是10%的东西吗?更多?减?

+0

您可以使用iOS 8提供的EdgeGestureRecognizer。这对您来说很简单。 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScreenEdgePanGestureRecognizer_class/index.html#//apple_ref/occ/cl/UIScreenEdgePanGestureRecognizer –

+0

不是真的。只有当我从边缘开始时,才会触发此手势。我需要能够在屏幕上的任何位置开始平移手势,最终达到边缘并最终离开这个边缘,仍然平移。 – Nico

+0

为什么不能获得屏幕:宽度然后查看touchesBegan,并获取当前CGPoint(用户触摸的位置)。设置你的范围,当x达到一个“x”值,然后发起一个动作。 – whereisleo

回答

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中。

+0

是的,这就是我想到的,只是想知道是否有更好的方法来做到这一点。 – Nico

相关问题