ios iPhone:在 MKMapView 中检测点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1275731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone: Detecting Tap in MKMapView
提问by ChrisJF
How do I detect a single tap on an instance of MKMapView
? Do I have to subclass MKMapView
and then override the touchesEnded
method?
如何检测对 的实例的单击MKMapView
?我是否必须子类化MKMapView
然后覆盖该touchesEnded
方法?
Thanks,
谢谢,
-Chris
-克里斯
采纳答案by tt.Kilew
Hope this will help : How to intercept touches events on a MKMapView or UIWebView objects?
希望这会有所帮助:如何拦截 MKMapView 或 UIWebView 对象上的触摸事件?
回答by pseudopeach
If you're just looking to get notified of tap gestures without affecting any of the other touch behavior of the map, you'll want to use a UITapGestureRecognizer
. It's super simple, just put in some code like this.
如果您只是想在不影响地图的任何其他触摸行为的情况下获得有关点击手势的通知,则需要使用UITapGestureRecognizer
. 超级简单,只要输入一些这样的代码即可。
UITapGestureRecognizer* tapRec = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(didTapMap:)];
[theMKMapView addGestureRecognizer:tapRec];
[tapRec release];
That will call the didTapMap
whenever theMKMapView
receives a tap gesture and all the pinching, and dragging gestures will still work as they did before.
这将调用didTapMap
每当theMKMapView
接收到点击手势和所有捏和拖动手势时,它们仍然会像以前一样工作。
回答by Sanjeev Rao
Working Perfectly on iOS 8
在 iOS 8 上完美运行
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
doubleTap.numberOfTapsRequired = 2;
doubleTap.numberOfTouchesRequired = 1;
[self.mapView addGestureRecognizer:doubleTap];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[singleTap requireGestureRecognizerToFail: doubleTap];
[self.mapView addGestureRecognizer:singleTap];
}
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
return;
//Do your work ...
}
回答by TimM
Or depending on what you are trying to do, add an MKAnnotation
(push pin, with a callout), so you have something to tap on - and then your map delegate will receive an event eg.
或者,根据您要执行的操作,添加一个MKAnnotation
(图钉,带有标注),以便您可以点击某些内容 - 然后您的地图委托将收到一个事件,例如。
mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
回答by Daniel
You cant at this time intercept touches on a map view, you can try layering an opaque view on there and see if it picks up touches...
你现在不能拦截地图视图上的触摸,你可以尝试在那里分层一个不透明的视图,看看它是否能拾取触摸......
回答by WINSergey
Just add some code snippet as illustration of @tt-kilew answer. In my case, I want to point the user to itself on the map but do not want to interrupt his drag touch.
只需添加一些代码片段作为@tt-kilew 答案的说明。就我而言,我想将用户指向地图上的自己,但不想中断他的拖动触摸。
@interface PrettyViewController () <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (assign, nonatomic) BOOL userTouchTheMap;
@end
@implementation PrettyViewController
#pragma mark - UIResponder
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
self.userTouchTheMap = [[touches anyObject].view isEqual:self.mapView];
}
#pragma mark - MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
//We just positioning to user
if (!self.userTouchTheMap) {
CLLocationDistance radius = 5000;
[self.mapView setRegion:MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 2*radius, 2*radius) animated:YES];
}
}
@end
回答by Livio
Nothing I ever found worked, but I came up with this unperfect solution : In viewDidLoad
我从来没有发现任何工作,但我想出了这个不完美的解决方案:在 viewDidLoad
let singleTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(onMapClicked))
singleTapRecognizer.delegate = self
mapView.addGestureRecognizer(singleTapRecognizer)
In delegate :
在委托中:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
return touch.view!.frame.equalTo(mapView.frame)
}
回答by ingconti
my 2 cents for swit 5.x:
我 swit 5.x 的 2 美分:
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let v = touch.view
let ssv = v?.superview?.superview
if ssv === self.mapView{
searchBar.resignFirstResponder()
}
}
}
it works. but honestly can break if apple changes layers of views. better a recognizer.
有用。但老实说,如果苹果改变视图层,就会崩溃。更好的识别器。