How to zoom an image without zooming sub views in iPhone?

北慕城南 提交于 2019-12-23 04:43:09

问题


I have scroll view with an UIImageView init.I add a button on UIImageView.When i zoom the image then my button is also perform zooming.How can i only zoom the Image not the button.Thanks in advance for you time.


回答1:


Place the button and any other views you do not want to be affected by the UIScrollView zooming in a view at the same level as the UIScrollView. Here's an example view hierarchy that was set up for just this purpose.

The scroll view and anything inside it will zoom (in this case the Image View). In the UIView below the scroll view (in this case, an image view with the Default.png image) will not zoom when the scroll view is zoomed.

EDIT:

To have the button remain in the same place relative to the image add it as a subview to your scroll view, add UIScrollViewDelegate to your view controller header. In your view controller implementation: capture the initial UIButton frame in viewWillAppear, and use the scroll view delegate scrollViewDidZoom method to update the button frame so that is stays in place. This will also allow the button to move with the image when panning.

Here's the updated view structure:

The applicable view controller code segments:

@interface MyViewController () {
    CGRect initialButtonFrame;

    ...
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    initialButtonFrame = self.button.frame;

    ...
}

- (void)scrollViewDidZoom:(UIScrollView *)sv
{

        self.button.frame = CGRectMake((initialButtonFrame.origin.x * self.scrollView.zoomScale),
                               (initialButtonFrame.origin.y * self.scrollView.zoomScale),
                               initialButtonFrame.size.width,
                               initialButtonFrame.size.height);

...
}

Here are a couple of images showing the button staying in place relative to its placement in the image. Note the button origin is near the top left of the red area near the center bottom of the fireman's coat when not zoomed (first image) and when zoomed (second image).




回答2:


Add the button on the imageScrollView superview after the imageScrollView so that the button is not scroll view's subview.



来源:https://stackoverflow.com/questions/12933196/how-to-zoom-an-image-without-zooming-sub-views-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!