我正在使用swift创建一个应用程序。在我的一个ViewController中,有一个我以编程方式创建的GMSMapView。我希望用户有能力触发一个行动时,点击地图。
我所做的:
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")
}
}我尝试过覆盖touchesBegan,但没有起作用。我已尝试插入mapView.userInteractionEnabled = true,但不起作用...
有什么想法吗?
发布于 2014-11-19 20:56:07
我成功地做到了
func mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) {
println("It works")
}但如果有人能向我解释为什么另一个解决方案不起作用,那就太好了!
发布于 2017-07-28 00:23:44
您可以使用默认的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;发布于 2014-11-19 21:12:02
地图视图已经有了自己的手势识别器,用于平移、缩放等。
因此,您可能需要告诉系统,它应该注意多个手势识别器。
作为UIGestureRecognizerDelegate协议的一部分:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}https://stackoverflow.com/questions/27016989
复制相似问题