resize all subview in scrollViewDidZoom

2019-01-19 08:37发布

问题:

Below is my screen design.

.

I want Zoom in/out in **mainView**. mainView Width & Height is 300. To Zoom in/out i have implement the below method and its working fine with this.

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return mainView;
}

Everything is fine up to this. I have set scroll.minimumZoomScale=1 and scroll.maximumZoomScale=5.

During Zoom in time mainView Frame increase depend on **scrollView.zoomScale**and i have check - (void)scrollViewDidZoom:(UIScrollView *)scrollView in this method.

At Zoom in time

**if i get scrollView.zoomScale=2 than mainView Width & height become 600
if i get scrollView.zoomScale=3 than mainView Width & height become 900**

During this process innerView resize due to autoresize property.But Width & Height of innerView is 100 (not change at zoom in/out time).

Can we change this according to scale ??

Finally what i want that Whenever innerView & lblTest frame change than i want to increase/decrease the numberOfLine of lblTest at zoom in/outtime.

I have tried to add innerView & lblTest manually but getting the issue that its Width & Height increase more than its superview (mainView).

Really appreciated If any know how to archive this .

Thanks in Advance.

回答1:

When you are zooming in the scroll view, the zoomView (mainView in your case) is not resized, just an affine transformation applied on it's layer (CALayer). For example in case of 1.2 zoomScale it is :[1.2, 0, 0, 1.2, 0, 0]. So it's bounds is not changed - just all of it's content scaled up/down, it also affects it's frame property - so nor the contained inner view, and label will be resized, just scaled up/down when rendering the mainView, this is why their frame is unaffected.

Just implement - (void)scrollViewDidZoom:(UIScrollView *)scrollView in the delegate and print out the main view's frame, bounds, layer.affineTransform to see what happens on zooming.

To make what you want you have to adjust the label according the zoomScale:

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    float zoomScale = scrollView.zoomScale;
    self.label.bounds = CGRectMake(0, 0, 100*zoomScale, 100*zoomScale); // supposing that label's original size is {100, 100}
    self.label.layer.affineTransform = CGAffineTransformMakeScale(1.0/zoomScale, 1.0/zoomScale);
}