滑动垂直手势无法使用UITableView

时间:2017-06-02 11:30:13

标签: ios uitableview swift3 uiswipegesturerecognizer

我有UIViewController,我在故事板中添加了一个UITableView,后来我在视图中添加了一个向上滑动手势识别器,但没有任何反应。

这是我的代码

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate {

@IBOutlet weak var tableview: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let swipeRegongnizer = UISwipeGestureRecognizer(target: self, action: #selector(self.handleSwipeUp))
    swipeRegongnizer.direction = UISwipeGestureRecognizerDirection.up
    swipeRegongnizer.delegate = self
    tableview.addGestureRecognizer(swipeRegongnizer)
}

func handleSwipeUp(gesture: UISwipeGestureRecognizer) {
        print("swiped up")
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    cell.textLabel?.text = "cell\(indexPath.row)"
    return cell
}
}

3 个答案:

答案 0 :(得分:10)

1。实施UIGestureRecognizerDelegate。 2。设置委托。

    yourGesture.delegate = self

3. 在相应的swift文件中添加以下功能。

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

答案 1 :(得分:2)

实施委托方法

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

此方法将允许同时识别表格视图的平移手势和滑动手势

答案 2 :(得分:0)

您需要在各自的视图控制器中添加以下委托。

<强>目标C

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
  return YES;
}

<强>夫特

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

解释

当gestureRecognizer或otherGestureRecognizer识别手势会阻止其他手势识别器识别其手势时,会调用此方法。请注意,保证返回YES可以同时识别;另一方面,返回NO不能保证防止同时识别,因为另一个手势识别器的代表可能return YES。默认情况下,它返回false