Chapter 7 Solution - Pinch to Zoom Silver Challenge w/Ouput


I did not see a need to declare a @property for the Hypnosister view to use across methods, I simply referenced the subview of scrollview via subscripting


zoom scale log


Create ScrollView
// A window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

CGRect screenRect = self.window.bounds;// Create a screen-sized scroll view
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect];[/code]

Configure scrollView
// Define ZOOM limits and multitouch, and delegate scrollView.maximumZoomScale = 5.0; // max zoom scale - required for zooming scrollView.minimumZoomScale = 1.0; // min zoom scale - required for zooming scrollView.multipleTouchEnabled = YES; // enable multi fingers for this view scrollView.delegate = self; // define delegate for zoom methods [self.window addSubview:scrollView]; // add it to window

Create Hypnosister view and add as subview to scrollview, return…
// Create a hypnosis view
ZGCHipnosisterView *hypnoView = [[ZGCHipnosisterView alloc] initWithFrame:screenRect];

// Add it to the scroll view
[scrollView addSubview:hypnoView];

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;


Protocol / Delegate Methods:

Using subscripting of scrollView.subviews array to return the subview (instead of using a @property to store the view)

[code]#pragma - scrollview delegate method #1

  • (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return scrollView.subviews[0];

Begin Zooming - using zoomToRect method of UIScrollView to commence zoom. Also logging starting zoom scale value (a GPFloat).

[code]#pragma - scrollview delegate method #2

  • (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
    [scrollView zoomToRect:view.frame animated:NO];
    NSLog(@“Current Zoom Scale: %f”, scrollView.zoomScale);

End Zooming - only using this method to Log end zoom scale value (moment fingers leave the screen)

[code]#pragma - scrollview delegate method #3

  • (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
    NSLog(@“Current Zoom Scale: %f”, scale);

Zoom in/out .mov

zoom scale log