缩放背景图片而不缩放叠加层

我是iOS开发的初学者,我试图展示一个需要可缩放和可移动的图像(实际上是一张图)。 我的问题是,我不知道如何添加一个覆盖,将遵循泛总是代表相同的位置,但不会缩放缩放。 会有几个叠加,每个必须是可点击的。

要缩放和平移图像(地图),我添加了我的UIImageViewUIScrollView ,它的工作很好,但我不知道如何添加此function。

我发现这个线程,但它不是真正的问题,因为他的覆盖是静态的: UIScrollview有两个图像 – 保持1图像可缩放和1图像静态(固定大小)

我已经在android中开发了相同的应用程序,为了使这个工作,我转换了屏幕坐标的地图像素感谢转换matrix,我覆盖了onDraw方法来显示它,但我不知道如何在iOS上做同样的事情,我不知道这是否是更好的解决scheme。

谢谢你的帮助。

好吧,所以我find了一个办法,如果它可以帮助任何人:

我添加了我的叠加在scrollView,与背景图像(地图)。

 +CustomScrollView ----> UIImageView (map) ----> OverlayImageView (overlay) 

为了缩放,自定义的scrollview需要一个委托与以下方法:

 - (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 ,所以它可以平移。 所以我们唯一需要关心的就是在缩放变化时移动叠加层,并且我们通过scrollViewDidZoom方法知道它。

为了处理触摸事件,我们重写CustomScrollViewtouchEnded:withEvent:如果只有一个水龙头,我们将它转​​发到覆盖。 我不显示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]; } } 

希望这会有所帮助。

您可以将imageView放在顶层,并将其userInteractionEnabled属性设置为NO。 那么你必须以编程方式平移它。