简体   繁体   English

如何在不缩放iPhone中的子视图的情况下缩放图像?

[英]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. 我有一个UIImageView init的滚动视图。我在UIImageView上添加一个按钮。当我缩放图像然后我的按钮也执行缩放。我怎么只能缩放图像而不是按钮。提前感谢您的时间。

在此输入图像描述

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 . 放置按钮,你不想被影响的其他意见UIScrollView在同一水平的一个视图缩放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. 在下面的UIView中,滚动视图(在这种情况下,带有Default.png图像的图像视图)在缩放滚动视图时不会缩放。

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. 要使按钮保持在相对于图像的相同位置,将其作为子视图添加到滚动视图,将UIScrollViewDelegate添加到视图控制器头。 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. 在视图控制器实现中:捕获viewWillAppear的初始UIButton帧,并使用滚动视图委托scrollViewDidZoom方法更新按钮帧以使其保持原位。 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). 请注意,当未放大(第一张图像)和放大(第二张图像)时,按钮原点靠近消防员外套中心底部附近红色区域的左上角。

在此输入图像描述

在此输入图像描述

imageScrollView之后添加imageScrollView imageScrollView上的按钮,使按钮不是滚动视图的子视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM