简体   繁体   English

GMSMapView中的UITapGestureRecogniser

[英]UITapGestureRecogniser in GMSMapView

I am creating an app using swift. 我正在使用swift创建一个应用程序。 In one of my ViewController, I have a GMSMapView that I create programatically. 在我的一个ViewController中,我有一个以编程方式创建的GMSMapView。 I want user to have the capability triggering an action when clicking on the map. 我希望用户能够在单击地图时触发操作。

What I have done : 我做了什么 :

import UIKit

class MapViewController: UIViewController, GMSMapViewDelegate {

let mapView = GMSMapView()

override func viewDidLoad() {
        super.viewDidLoad()

        mapView.delegate = self
        mapView.settings.scrollGestures = false
        mapView.frame = CGRectMake(0, 65, 375, 555)
        view.addSubview(mapView)


        var tap = UITapGestureRecognizer(target: self, action: "tap:")
        mapView.addGestureRecognizer(tap)
}

func tap(recogniser:UITapGestureRecognizer)->Void{
        println("it works")
    }

}

I have tried to override touchesBegan, didnt work. 我试图覆盖touchesBegan,没有工作。 I have tried to insert mapView.userInteractionEnabled = true, didnt work... 我试图插入mapView.userInteractionEnabled = true,没有工作......

Any idea? 任何的想法?

I managed to do it with 我设法做到了

func mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) {
        println("It works")
    }

But if someone could explain to me why the other solution didn't work, it would be great! 但如果有人能向我解释为什么其他解决方案不起作用,那就太好了!

You can use default MapVIew LongPress event 您可以使用默认的MapVIew LongPress事件

  /**

 * Called after a long-press gesture at a particular coordinate.
 *
 * @param mapView The map view that was pressed.
 * @param coordinate The location that was pressed.
 */
     - (void)mapView:(GMSMapView *)mapView
    didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate;

The map view already has its own gesture recognizers for panning, zooming etc. 地图视图已经拥有自己的手势识别器,可用于平移,缩放等。

So you probably need to tell the system that it should take care on multiple gesture recognizers. 所以你可能需要告诉系统它应该注意多个手势识别器。

As part of the UIGestureRecognizerDelegate protocol: 作为UIGestureRecognizerDelegate协议的一部分:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM