我想在UITableView中添加两个手指向上和向下滑动手势。 我们的想法是使用一个手指摇动手势滚动单元格,并使用两个手指向上/向下滑动手势来执行其他操作。 我希望通过Tweetbot的夜间模式切换获得类似的体验:https://vine.co/v/hF5J1Y7hubT
这是我的代码:
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
,那么当我使用两根手指时为什么不会失败?
你知道如何让这些手势相互影响吗?
答案 0 :(得分:-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。这将关注是否正在处理滑动或平移。