2015-04-05 76 views
0

在我的ViewController中,我有一個UIScreenEdgeGestureRecognizer用於切換視圖,但也有一個UITableView用於定製UITableViewCell。在這個定製UITableViewCell是一個UIPanGestureRecognizer刷卡細胞。檢測UIScreenEdgeGestureRecognizer並失敗UIPanGestureRecognizer

我加了gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:所以兩個手勢都在工作,並且gestureRecognizerShouldBegin可以防止垂直滾動的衝突。

問題是,我怎麼能給UIScreenEdgeGestureRecognizer優先?當我在屏幕邊緣滑動時,我想切換視圖而不移動單元格。我想我應該使用委託方法,但到目前爲止,我讀得越多,我越感到困惑。

override func viewDidload() { 
    var recognizer = UIPanGestureRecognizer(target: self, action: "handlePan:") 
    recognizer.delegate = self 
    addGestureRecognizer(recognizer) 
} 

override func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { 
    return true 
} 

override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool { 
    if let panGesture = gestureRecognizer as? UIPanGestureRecognizer { 
     let velocity = panGesture.velocityInView(superview!) 
     if fabs(velocity.x) > fabs(velocity.y) { return true } 
     return false 
    } 
    return false 
} 

的ViewController:

定製UITableViewCell代碼

override func viewDidLoad() { 
    let rightScreenEdgeRecognizer = EdgePanGesture(target: self, action: "changeView:") 
    rightScreenEdgeRecognizer.edges = .Right 
    view.addGestureRecognizer(rightScreenEdgeRecognizer) 
} 

我也試過標識與識別:

gestureRecognizer.isKindOfClass(UIScreenEdgePanGestureRecognizer) 

gestureRecognizer as? UIScreenEdgePanGestureRecognizer 

但都失敗了。

回答

0

原來我正在用gestureRecognizer as? UIScreenEdgePanGestureRecognizer看正確的方向,而不是gestureRecognizer我應該使用otherGestureRecognizer

override func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOfGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { 
    if let recognizer = otherGestureRecognizer as? UIScreenEdgePanGestureRecognizer { 
     return true 
    } 
    return false 
}