当我将UITapGestureRecognizer附加到UITableViewCell中的元素时,为什么它不起作用?

时间:2016-10-18 17:17:09

标签: ios swift uitableview mapkit uitapgesturerecognizer

我想覆盖双击mapView的默认行为。

在我的swift应用中,我在静态单元格中有mapView,因此在方法cellForRowAt中,我决定添加UITapGestureRecognizer。我就是这样做的:

func tableView(_ myTable: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if (indexPath as NSIndexPath).row == 0 {
        let cell = myTable.dequeueReusableCell(withIdentifier: "cellStatic") as! MyTableDetailsCell

        cell.mapView.isScrollEnabled = false //this works

        //this does not:
        let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
        tap.numberOfTapsRequired = 2
        cell.mapView.addGestureRecognizer(tap)
        ...

然后我有一个简单的功能:

func doubleTapped() {
    print("map tapped twice")
}

但是当我点击两次地图时 - 它会放大并且控制台日志中没有打印 - 所以我的代码不起作用。我错过了什么?

3 个答案:

答案 0 :(得分:2)

您必须强制执行自己的双击手势识别器禁用> db.test.aggregate({ $project: { type: 1, details: 1, at: 1, weight: { $cond: [ { "$or": [ {$eq: ["$type", "One"] }, {$eq: ["$type", "Two"] } ] }, 0, 1] } } }, {$sort: {weight: 1}}, { $limit : 5 }, {$project: {type: 1, details: 1, at: 1} }); { "_id" : ObjectId("58067329a7518db4d3d2e973"), "type" : "One", "details" : "This is one", "at" : "some place, where one is relevant" } { "_id" : ObjectId("5806733da7518db4d3d2e974"), "type" : "Two", "details" : "This is two", "at" : "some place, where one is relevant" } { "_id" : ObjectId("58067374a7518db4d3d2e976"), "type" : "Four", "details" : "This is four", "at" : "some place, where one is relevant" } { "_id" : ObjectId("58067381a7518db4d3d2e977"), "type" : "Five", "details" : "This is five", "at" : "some place, where one is relevant" } { "_id" : ObjectId("5806734ba7518db4d3d2e975"), "type" : "Three", "details" : "This is three", "at" : "some place, where one is relevant" } 的标准双击手势识别器。
这可以使用委托方法完成:

使用mapView

将视图控制器声明为手势识别器的代理

为您自己的双击手势识别器定义属性:

UIGestureRecognizerDelegate

设置双击手势识别器,例如在var myDoubleTapGestureRecognizer: UITapGestureRecognizer?

viewDidLoad

注意,委托是在这里设置的。

实施以下委托方法:

myDoubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
myDoubleTapGestureRecognizer!.numberOfTapsRequired = 2
myDoubleTapGestureRecognizer!.delegate = self
mapView.addGestureRecognizer(myDoubleTapGestureRecognizer!)

因此,当您双击func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool { if ((gestureRecognizer == myDoubleTapGestureRecognizer) && (otherGestureRecognizer is UITapGestureRecognizer)) { let otherTapGestureRecognizer = otherGestureRecognizer as! UITapGestureRecognizer return otherTapGestureRecognizer.numberOfTapsRequired == 2 } return true } 时,如果另一个手势识别器是mapView的内置双击识别器,则此委托方法将返回true。这意味着只有当您自己的双击识别器无法识别双击时,内置双击识别器才会触发。 我测试了它:地图不再缩放,并调用方法mapView

答案 1 :(得分:0)

尝试使用touchesBegan来识别触摸事件,并且可以在事件被触发时调用自定义处理程序

答案 2 :(得分:0)

将mapview添加为tableViewCell中容器视图的子视图。设置约束,以便mapview填充entier容器视图。禁用mapview的用户交互,并将双击手势添加到容器视图。这段代码会有所帮助。

    let cell = myTable.dequeueReusableCell(withIdentifier: "cellStatic") as! MyTableDetailsCell
    let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
    cell.mapView.isUserInteractionEnabled = false
    cell.containerView.addGestureRecognizer(tap)
    tap.numberOfTapsRequired = 2

现在" doubleTapped"轻击两次mapview时将调用selector。所有其他用户交互(包括地图视图的旋转手势)均已禁用。