2015-11-05 82 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

相關問題