滑动垂直手势无法使用UITableView

问题描述 投票:2回答:3

我有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
}
}
ios uitableview swift3 uiswipegesturerecognizer
3个回答
10
投票

1.实现UIGestureRecognizerDelegate。 2.设置代表。

    yourGesture.delegate = self

3.在各自的swift文件中添加以下功能。

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

2
投票

实现委托方法

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

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


0
投票

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

Objective-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

© www.soinside.com 2019 - 2024. All rights reserved.