5

我想添加兩個手指在UITableView中上下滑動手勢。 想法是使用一個手指平移手勢滾動單元格,並通過使用兩個手指向上/向下滑動手勢來執行其他操作。 我想達到類似的經驗,Tweetbot的夜間模式切換:https://vine.co/v/hF5J1Y7hubT識別兩個手指向下滑動UITableView中的手勢

這是我的代碼:

func setupGestureRecognizer() { 

    swipeUp = UISwipeGestureRecognizer(target: self, action: "handleSwipe") 
    swipeDown = UISwipeGestureRecognizer(target: self, action: "handleSwipe") 

    swipeUp.direction = UISwipeGestureRecognizerDirection.Up 
    swipeDown.direction = UISwipeGestureRecognizerDirection.Down 

    swipeUp.numberOfTouchesRequired = 2 
    swipeDown.numberOfTouchesRequired = 2 

    self.tableView.panGestureRecognizer.maximumNumberOfTouches = 1 

    self.tableView.panGestureRecognizer.requireGestureRecognizerToFail(swipeUp) 
    self.tableView.panGestureRecognizer.requireGestureRecognizerToFail(swipeDown) 

    self.tableView.addGestureRecognizer(swipeUp) 
    self.tableView.addGestureRecognizer(swipeDown) 

} 

func handleSwipe() { 
    print("Swiped!") 
    let alert = UIAlertController(title: "Gesture recognizer", message: "Swipe detected", preferredStyle: UIAlertControllerStyle.Alert) 
    let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil) 
    alert.addAction(action) 
    self.presentViewController(alert, animated: true, completion: nil) 
} 

setupGestureRecognizer()被稱爲viewDidLoad()

我得到我的警報時,我向上輕掃或用兩根手指向下,但是當我使用平移手勢時,在桌子移動之前有一個明顯的滯後。這可能是時間平移手勢需要等待,以確保刷卡手勢失敗:

它實際上對我來說更有意義requireGestureRecognizerToFail設置爲:swipeDown.requireGestureRecognizerToFail(self.tableView.panGestureRecognizer),但是當我嘗試它時,滑動手勢根本不起作用。我認爲panGestureRecognizer有問題。如果我明確表示它應該接受maximumNumberOfTouches = 1,爲什麼不使用兩根手指失敗?

你知道如何讓這些手勢互相影響嗎?

回答

-1

刪除這些行

self.tableView.panGestureRecognizer.requireGestureRecognizerToFail(swipeUp) 
self.tableView.panGestureRecognizer.requireGestureRecognizerToFail(swipeDown) 

設置的動作委託swipeUp和swipeDown

swipeDown.delegate = self 
swipeUp.delegate = self 

,並動態處理失敗的手勢代表。

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

總是返回true。這將處理是否正在處理滑動或平移。