I have a UIView
inside a UIScrollView
. Whenever the UIScrollView
zoom changes, I want to redraw the entire UIView
at the new zoom level.
In iOS < 3.2, I was doing this by resizing the UIView
within the UIScrollView
to make it the new size, and then setting the transform back to Identity, so that it wouldn't try to resize it further. However, with iOS >= 3.2, changing the identity also changes the UIScrollView
's zoomScale property.
The result is that whenever I zoom (say 2x), I adjust the embedded UIView
to be the appropriate size, and redraw it. However now (since I reset the transform to Identity), the UIScrollView
thinks its once again at zoomScale 1, rather than zoomScale 2. So if I have my maxZoomScale set at 2, it will still try zooming further, which is wrong.
I thought about using the CATiledLayer
, but I don't think this is sufficient for me, since I want to redraw after every zoom, not just at certain zoom thresholds like it tries to do.
Does anyone know how to do the proper redrawing of the UIView
on a zoom?
Tom,
Your question is a bit old, but I came up with a solution for this, so I figured I'd put in an answer in case it helps you or anyone else. The basic trick is to reset the scroll view's
zoomScale
to 1, and then adjust theminimumZoomScale
andmaximumZoomScale
so that the user can still zoom in and out as expected.In my implementation, I've subclassed
UIScrollView
and set it to be its own delegate. In my subclass, I implement the two delegate methods you need for zooming (shown below). contentView is a property I added to myUIScrollView
subclass that in order to give it the view that actually displays the content.So, my init method looks something like this (
kMinimumZoomScale
andkMaximumZoomScale
are#define
's at the top of the class):Then I implement the standard
UIScrollView
delegate methods for zooming. MyContentView
class has a property called zoomScale that tells it what scale to use for displaying its content. It uses that in itsdrawRect
method to resize the content as appropriate.In my case, I have an image view and on top of the image view I have several other imageviews. This is the implementation I came up with and works fine: