I want to override the default behavior of double tapping the mapView.
我想覆盖双击mapView的默认行为。
In my swift
app I have a mapView
in a static cell, so in the method cellForRowAt
I've decided to add a UITapGestureRecognizer
. This is how I do it:
在我的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)
...
And then I have a simple function:
然后我有一个简单的功能:
func doubleTapped() {
print("map tapped twice")
}
But when I tap twice the map - it zooms in and there's no print in the console log - so my code doesn't work. What did I miss?
但是当我点击两次地图时 - 它会放大并且控制台日志中没有打印 - 所以我的代码不起作用。我错过了什么?
3 个解决方案
#1
2
You had to enforce that your own double tap gesture recognizer disables the standard double tap gesture recognizer of the mapView
.
This can be done using a delegate method:
您必须强制执行您自己的双击手势识别器禁用mapView的标准双击手势识别器。这可以使用委托方法完成:
Declare your view controller as a delegate of a gesture recognizer, using UIGestureRecognizerDelegate
使用UIGestureRecognizerDelegate将视图控制器声明为手势识别器的委托
Define a property for your own double tap gesture recognizer:
为您自己的双击手势识别器定义属性:
var myDoubleTapGestureRecognizer: UITapGestureRecognizer?
Set up your double tap gesture recognizer, e.g. in viewDidLoad
:
设置双击手势识别器,例如在viewDidLoad中:
myDoubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
myDoubleTapGestureRecognizer!.numberOfTapsRequired = 2
myDoubleTapGestureRecognizer!.delegate = self
mapView.addGestureRecognizer(myDoubleTapGestureRecognizer!)
Note, that the delegate is set here.
请注意,委托是在此处设置的。
Implement the following delegate method:
实现以下委托方法:
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
}
So, when you double tap the mapView
, this delegate method returns true
if the other gesture recognizer is the built-in double tap recognizer of the mapView
. This means that the built-in double tap recognizer can only fire if your own double tap recognizer fails to recognize a double tap, which it won’t.
I tested it: The map is no longer zoomed, and method doubleTapped
is called.
因此,当您双击mapView时,如果另一个手势识别器是mapView的内置双击识别器,则此委托方法返回true。这意味着内置双击识别器只有在您自己的双击识别器无法识别双击时才能触发,而不会识别。我测试了它:地图不再被缩放,并调用方法doubleTapped。
#2
0
Try using touchesBegan to identify the touch event, and you can call your custom handler when the event is trigerred
尝试使用touchesBegan来识别触摸事件,并且可以在事件被触发时调用自定义处理程序
#3
0
Add the mapview as a subview of a container view in the tableViewCell. Set constraints so that the mapview fills the entier container view. Disable the user interaction of the mapview and add the double tap gesture to the container view. This code will help.
将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
Now the "doubleTapped" selector will be called when the mapview is tapped twice. All other user interactions including the rotation gesture of the mapview are disabled.
现在,当轻击mapview两次时,将调用“doubleTapped”选择器。所有其他用户交互(包括mapview的旋转手势)都被禁用。
#1
2
You had to enforce that your own double tap gesture recognizer disables the standard double tap gesture recognizer of the mapView
.
This can be done using a delegate method:
您必须强制执行您自己的双击手势识别器禁用mapView的标准双击手势识别器。这可以使用委托方法完成:
Declare your view controller as a delegate of a gesture recognizer, using UIGestureRecognizerDelegate
使用UIGestureRecognizerDelegate将视图控制器声明为手势识别器的委托
Define a property for your own double tap gesture recognizer:
为您自己的双击手势识别器定义属性:
var myDoubleTapGestureRecognizer: UITapGestureRecognizer?
Set up your double tap gesture recognizer, e.g. in viewDidLoad
:
设置双击手势识别器,例如在viewDidLoad中:
myDoubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
myDoubleTapGestureRecognizer!.numberOfTapsRequired = 2
myDoubleTapGestureRecognizer!.delegate = self
mapView.addGestureRecognizer(myDoubleTapGestureRecognizer!)
Note, that the delegate is set here.
请注意,委托是在此处设置的。
Implement the following delegate method:
实现以下委托方法:
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
}
So, when you double tap the mapView
, this delegate method returns true
if the other gesture recognizer is the built-in double tap recognizer of the mapView
. This means that the built-in double tap recognizer can only fire if your own double tap recognizer fails to recognize a double tap, which it won’t.
I tested it: The map is no longer zoomed, and method doubleTapped
is called.
因此,当您双击mapView时,如果另一个手势识别器是mapView的内置双击识别器,则此委托方法返回true。这意味着内置双击识别器只有在您自己的双击识别器无法识别双击时才能触发,而不会识别。我测试了它:地图不再被缩放,并调用方法doubleTapped。
#2
0
Try using touchesBegan to identify the touch event, and you can call your custom handler when the event is trigerred
尝试使用touchesBegan来识别触摸事件,并且可以在事件被触发时调用自定义处理程序
#3
0
Add the mapview as a subview of a container view in the tableViewCell. Set constraints so that the mapview fills the entier container view. Disable the user interaction of the mapview and add the double tap gesture to the container view. This code will help.
将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
Now the "doubleTapped" selector will be called when the mapview is tapped twice. All other user interactions including the rotation gesture of the mapview are disabled.
现在,当轻击mapview两次时,将调用“doubleTapped”选择器。所有其他用户交互(包括mapview的旋转手势)都被禁用。