我想重写双击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")
}但是当我点击两次地图时--它放大了,控制台日志中没有打印--所以我的代码无法工作。我错过了什么?
发布于 2016-10-18 18:58:42
您必须强制执行您自己的双点击手势识别器禁用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时,如果其他手势识别器是mapView的内置双抽头识别器,则此委托方法返回mapView。这意味着内置的双点击识别器只能在您自己的双点击识别器无法识别双抽头时才能触发,而它不会。
我测试了它:地图不再缩放,方法doubleTapped被调用。
发布于 2016-10-18 18:29:00
尝试使用touchesBegan标识触摸事件,当事件发生时,您可以调用自定义处理程序。
发布于 2016-10-18 19:00:01
将mapview添加为tableViewCell中容器视图的子视图。设置约束,以便映射填充实体容器视图。禁用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“选择器。所有其他用户交互,包括地图视图的旋转姿态都被禁用。
https://stackoverflow.com/questions/40114326
复制相似问题