当我双击MKMapView时,我正试图达到与优步应用相同的效果。
它不会放大用户点击的点,而是放在地图的当前中心。
这是我到目前为止所尝试的内容:
但是,即使删除原始的双击手势识别器似乎也能正常工作,我的新手势识别器选择器也不会被触发。
- (void)viewDidLoad
{
[self removeDefaultDoubleTapToZoomGestureRecognizer];
[self addCustomDoubleTapToZoomGestureRecognizer];
}
- (void)removeDefaultDoubleTapToZoomGestureRecognizer
{
[self findAndRemoveDefaultDoubleTapToZoomInView:self.map];
}
- (void)findAndRemoveDefaultDoubleTapToZoomInView:(UIView *)view
{
NSArray *gestureRecognizers = view.gestureRecognizers;
for (UIGestureRecognizer *gestureRecognizer in gestureRecognizers)
{
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]])
{
UITapGestureRecognizer *tapGestureRecognizer = (UITapGestureRecognizer *)gestureRecognizer;
if (tapGestureRecognizer.numberOfTapsRequired == 2 && tapGestureRecognizer.numberOfTouchesRequired == 1)
{
[view removeGestureRecognizer:tapGestureRecognizer];
}
}
}
for (UIView *subView in view.subviews)
{
[self findAndRemoveDefaultDoubleTapToZoomInView:subView];
}
}
- (void)addCustomDoubleTapRecognizer
{
UIView *view = self.mapView;
UITapGestureRecognizer *gestureRecognizer = self.customDoubleTapGestureRecognizer;
[view addGestureRecognizer:gestureRecognizer];
}
- (IBAction)customDoubleTapGestureRecognizerTapped:(UITapGestureRecognizer *)sender
{
NSLog(@"double tap");
}
已从IB添加customDoubleTapGestureRegonizer
属性和-customDoubleTapGestureRecognizerTapped:
方法。
任何帮助都将不胜感激。
答案 0 :(得分:4)
我正在做一些与你非常相似的事情,它对我有用。如果可能,我会从IB中删除双击识别器逻辑,而是从addCustomDoubleTapRecognizer
实施中手动添加它
- (void)addCustomDoubleTapRecognizer {
UIView *view = self.mapView;
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zoomInGesture)];
[view addGestureRecognizer:gestureRecognizer];
gestureRecognizer.numberOfTapsRequired = 2;
}
然后添加zoomInGesture
方法,例如
- (void)zoomInGesture {
MKCoordinateRegion region = self.mapView.region;
MKCoordinateSpan span = self.mapView.region.span;
span.latitudeDelta *= 0.5;
span.longitudeDelta *= 0.5;
region.span = span;
[self.mapView setRegion:region animated:YES];
}
它适用于我,当用户双击地图上的任何地方时,它会放大当前地图中心。
答案 1 :(得分:1)
您确定customDoubleTapGestureRecognizer
已正确初始化了吗?
MKMapView
中),你的代码可以在以后的任何更新中中断,等等。我会添加空的UIView
叠加层(与{{1}相同的帧) })使用双击手势识别器,以免弄乱MKMapView
内部。