ScrollView Delegation


#1

I still have a doubt about how delegation actually works in that what are the internal mechanics of it.
In the Hypnosister app, we made HypnosisterAppDelegate a delegate of UIScrollView— [scrollView setDelegate:self]. where self is an object of HypnosisterAppDelegate.
Then we do this— [scrollView setMinimumZoomScale:1.0] and then [scrollView setMaximumZoomScale:5.0].
when these messages are passed to the scrollView object and run does -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView get called internally?? I mean do these setter methods internally invoke this method in their code??
Otherwise where else does -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView get called if not from [scrollView setMinimumZoomScale:1.0] and then again from [scrollView setMaximumZoomScale:5.0]. sequentially


#2

Once the scrollView has a reference to your delegate it can use it (invoke methods, access properties, etc.) any time it likes.
Whenever the scrollView needs to know what view is supposed to be the one that is scaled it will call viewForZoomingInScrollView

I guess exactly when that happens is up to the scrollView and is an implementation detail that we don’t get to see - it could be called once and cached or called everywhere in the scrollView’s code that requires it. As long as the behavior is maintained the framework developers could change the exact point at which it’s called between iOS releases too.

To find out when a delegate is invoked you can put a breakpoint in your method and see from the stack trace where it is being called from (you may need to use the slider to increase the depth of the trace to get a full picture).

In this case it looks like viewForZoomingInScrollView is first called from the UIScroll object’s zoomScale property from a pinch gesture event handler. This in turn is called in response to the gesture event being sent from the application.
So it looks like the app detects the pinch on the screen, sends a pinch gesture event which is picked up by the scroll view. At that point the scroll view needs to know which inner view is supposed to be scaled so it asks your code via the delegate.