好的,所以我找到了一种方法,如果它可以帮助任何人:
我在滚动视图中添加了我的叠加层,带有背景图像(地图)。
+CustomScrollView
----> UIImageView (map)
----> OverlayImageView (overlay)
为了缩放,自定义滚动视图需要一个具有以下方法的委托:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
//The background image with the map
return mapView;
}
//When a zoom occurs, move the overlay
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
UIImageView* overlayView = [scroll.subviews objectAtIndex:1];
float x;
float y;
float width;
float height;
//keep the width and height of the overlay
width = overlayView.frame.size.width;
height = overlayView.frame.size.height;
//Move it to stay over the same pixel of the map, and centers it
x = (self.overlay.point.x * scroll.zoomScale - width/2);
y = (self.overlay.point.y * scroll.zoomScale - height/2);
overlayView.frame = CGRectMake(x,y,width,height);
}
有了这个,我们说缩放只发生在背景图像上,但由于覆盖在 中UIScrollView
,它会随之平移。所以我们唯一需要关心的是在缩放变化时移动Overlay,我们通过scrollViewDidZoom
方法知道它。
为了处理触摸事件,我们覆盖了touchEnded:withEvent:
of CustomScrollView
,如果只有一次点击,我们将其转发到覆盖层。我没有展示,OverlayImageView
因为它只覆盖这个相同的方法 ( toucheEnded:withEvent:
) 来处理它的触摸。
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
// Coordinates in map view
CGPoint point = [touch locationInView:[self.subviews objectAtIndex:0]];
//forward
if(touch.tapCount == 1){
OverlayImageView* overlayView = [self.subviews objectAtIndex:1];
CGPoint newPoint = [touch locationInView:overlayView];
BOOL isInside = [overlayView pointInside:newPoint withEvent:event];
if(isInside){
[overlayView touchesEnded:touches withEvent:event];
}
}
// zoom
else if(touch.tapCount == 2){
if(self.zoomScale == self.maximumZoomScale){
[self setZoomScale:[self minimumZoomScale] animated:YES];
} else {
CGRect zoomRect = [self zoomRectForScrollView:self withScale:self.maximumZoomScale withCenter:point];
[self zoomToRect:zoomRect animated:YES];
//[self setZoomScale:[self maximumZoomScale] animated:YES];
}
[self setNeedsDisplay];
}
}
希望这会有所帮助。