Objective C - Cannot zoom UIImageView that is embedded into UIScrollView

I'm a new kid on the block here. Nice to meet you all.

I'm trying to implement Ray Wenderlich's zooming of an UIScrollView that was described here:

http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content

But it seemed like it didn't work for me. What I would like to do is:

Here's my code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //Hide the status bar
    [[UIApplication sharedApplication] setStatusBarHidden:YES];

    //------------------------------
    //Initialize the image view of the home screen
    _mapView = (TM_MapView *)[[[NSBundle mainBundle]loadNibNamed:@"MapView" owner:self options:nil]objectAtIndex:0];

    // set the content size to be the size our our whole frame
    mapScrollView.contentSize = _mapView.imageView.image.size;
    [mapScrollView setScrollEnabled:YES];

    //set the selector for the buttons in the map view
    [self setButtonsSelector];

    // now add our scroll view to the main view
    [mapScrollView addSubview:_mapView];
    //------------------------------

    NSLog(@"_mapView.imageView.frame: %@", NSStringFromCGRect( _mapView.imageView.frame));
    NSLog(@"_mapView.imageView.image.size: %@", NSStringFromCGSize( _mapView.imageView.image.size));
NSLog(@"mapScrollView.contentSize: %@", NSStringFromCGSize( mapScrollView.contentSize));

}

-(void)viewWillAppear:(BOOL)animated
{
    mapScrollView.minimumZoomScale = 1.0f;
    mapScrollView.maximumZoomScale = 2.5f;
    mapScrollView.zoomScale = 1.0f;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        [self zoomOnStart];
    }
}

-(void)zoomOnStart
{
    NSLog(@"Zooming on start");
    CGPoint zoomPoint = CGPointMake(512.0f, 384.0f);

    CGFloat newZoomScale = 2.0f;

    CGSize scrollViewSize = self.mapScrollView.bounds.size;

    CGFloat w = scrollViewSize.width / newZoomScale;
    CGFloat h = scrollViewSize.height / newZoomScale;
    CGFloat x = zoomPoint.x - (w/2.0f);
    CGFloat y = zoomPoint.y - (h/2.0f);

    CGRect rectToZoomTo = CGRectMake(x, y, w, h);
    //CGRect rectToZoomTo = CGRectMake(300, 300, 200, 100);

    NSLog(@"rectToZoomTo: %@", NSStringFromCGRect(rectToZoomTo));

    [self.mapScrollView zoomToRect:rectToZoomTo animated:YES];

}

#pragma mark - Delegates
-(UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView
{
    NSLog(@"Scroll View viewForZoomingInScrollView");
    return _mapView;
}

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    NSLog(@"Scroll View End Zooming!");
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    NSLog(@"Scroll View Zoom changed to: %f",  scrollView.zoomScale);
}

As you can see, I've logged some of the frames and delegates to see the input for the zooming and to see whether or not we are zooming. Here's the console logs:

2014-10-09 17:24:18.507 TrueMuzeiOS[13268:60b] _mapView.imageView.frame: {{0, 0}, {1024, 768}}
2014-10-09 17:24:18.511 TrueMuzeiOS[13268:60b] _mapView.imageView.image.size: {1024, 768}
2014-10-09 17:24:18.513 TrueMuzeiOS[13268:60b] mapScrollView.contentSize: {1024, 768}
2014-10-09 17:24:18.519 TrueMuzeiOS[13268:60b] Zooming on start
2014-10-09 17:24:18.521 TrueMuzeiOS[13268:60b] rectToZoomTo: {{320, 128}, {384, 512}}

As you can see, the logs inside viewForZoomingInScrollView:, scrollViewDidEndZooming:withView:atScale:, and scrollViewDidZoom: didn't get called, meaning we are not zooming at all (CMIIW). I've added the UIScrollViewDelegate so it should've worked.

So, can you guys help me here? I've followed the steps correctly, IMHO. So I'm lost now. Thanks a lot in advance.

Upvotes: 2

Views: 642

Answers (1)

cris
cris

Reputation: 333

do you set the delegate property of the UIScrollView?

I don´t see this on the code you published although you could have done it on Interface Builder.

If this the case, simply this would make your methods get called:

mapScrollView.delegate = self;

Check the UIScrollView documentation on the delegate property section to more information.

https://developer.apple.com/library/ios/documentation/uikit/reference/uiscrollview_class/index.html#//apple_ref/occ/instp/UIScrollView/delegate

Upvotes: 2

Related Questions