Mohd Haider
Mohd Haider

Reputation: 703

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

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.

enter image description here

Upvotes: 0

Views: 2396

Answers (2)

bobnoble
bobnoble

Reputation: 5824

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.

enter image description here

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:

enter image description here

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).

enter image description here

enter image description here

Upvotes: 3

A-Live
A-Live

Reputation: 8944

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

Upvotes: 0

Related Questions