zoomToRect does nothing if your UIScrollView is al

2019-06-24 09:06发布

问题:

I think I have found a bug in UIScrollView, but I want to check if other people are observing the same thing and if it is expected behaviour.

I've got a UIScrollView and am trying to set what it is looking at using zoomToRect. That works fine. If I then drag the view so it's looking elsewhere and then trigger the zoomToRect with the same values it does nothing. The zoomToRect command only does something if the zoomScale needs to be adjusted, if a pan is all that is needed the view just sits still. Since a user could pinch to zoom in or out I was just hoping to use zoomToRect and not have to double check the zoomLevels would be different.

has anyone else experienced this? Should it happen that way or is it a bug?

***Adding code as requested

First call to zoomToRect

[myScrollView zoomToRect:zoomToRect animated:YES];

View aimed correctly at point in the UIScrollView that I wanted it to be

Now let the user drag on the screen and move the point the UIScrollView is looking at. No zooming occurs, only panning.

Now call zoomToRect again, with the same rect

[myScrollView zoomToRect:zoomToRect animated:YES];

Nothing happens.

回答1:

Apple have said this is a known bug with UIScrollView and will be fixed in a future release.

https://devforums.apple.com/message/158712#158712

In the mean time I have worked around this as follows

float zoomBefore = myScrollView.zoomScale;
[myScrollView zoomToRect:zoomToRect animated:YES];
float zoomAfter = myScrollView.zoomScale;
if (zoomBefore == zoomAfter)
{
    [myScrollView setContentOffset:imageCoords animated:YES];
}


回答2:

Default maximumZoomScale is 1.0. So it doesn't zoom when we double click. set maximumZoomScale like this:

myScrollView.maximumZoomScale = 5.0f;