我正在尝试实现与Uber相同的地图行为。 当用户使用捏合或双击缩放地图时,我想保持地图居中。 我尝试实现自己的手势识别器,但结果不是很好...... 有没有人知道如何做到这一点?
谢谢!
答案 0 :(得分:1)
对于那些对解决方案感兴趣的人,我设法做到了。
首先,您需要在viewDidload中添加手势识别器
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchOnMap:)];
[pinch setDelegate:self];
[pinch setDelaysTouchesBegan:YES];
[self.mapGestureContainer addGestureRecognizer:pinch];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zoomInGesture:)];
gestureRecognizer.delegate = self;
[gestureRecognizer setDelaysTouchesBegan:YES];
gestureRecognizer.numberOfTapsRequired = 2;
[self.mapGestureContainer addGestureRecognizer:gestureRecognizer];
然后实现手势功能,它将根据捏缩比例处理缩放。我添加了一个小延迟以避免过多处理。
-(void) zoomInGesture:(UITapGestureRecognizer *) recognizer {
currentRegion = self.mapView.region;
currentSpan = self.mapView.region.span;
isZoomingWithDoubleTap = YES;
MKCoordinateRegion region = currentRegion;
MKCoordinateSpan span = currentSpan;
span.latitudeDelta = currentSpan.latitudeDelta / 2.3;
span.longitudeDelta = currentSpan.longitudeDelta / 2.3;
region.span = span;
[self.mapView setRegion:region animated:YES];
}
//Gesture used when the user pinch the area of the map
- (void) pinchOnMap:(UIPinchGestureRecognizer *) recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
isPinching = YES;
self.mapView.scrollEnabled = NO;
self.mapView.userInteractionEnabled = NO;
currentRegion = self.mapView.region;
currentSpan = self.mapView.region.span;
lastZoomTime = [[NSDate date] timeIntervalSince1970];
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
isPinching = NO;
self.mapView.scrollEnabled = YES;
self.mapView.userInteractionEnabled = YES;
}
if (recognizer.state == UIGestureRecognizerStateChanged) {
if (([[NSDate date] timeIntervalSince1970] * 1000) - lastZoomTime >= 20) {
lastZoomTime = [[NSDate date] timeIntervalSince1970] * 1000;
MKCoordinateRegion region = currentRegion;
MKCoordinateSpan span = currentSpan;
span.latitudeDelta = currentSpan.latitudeDelta / recognizer.scale;
span.longitudeDelta = currentSpan.longitudeDelta / recognizer.scale;
region.span = span;
[self.mapView setRegion:region animated:NO];
}
}
}
答案 1 :(得分:0)
在地图视图委托中,执行此操作
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
[mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
}