6

I got a photo gallery app an have an issue getting ready for ios 8. The images are displayed in a UIPageViewController and every page has a view with a custom UIScrollView class. It's just like Apples PhotoScroller Sample (https://developer.apple.com/library/ios/samplecode/PhotoScroller/Introduction/Intro.html)

I added a UITapGestureRecognizer for double tapping. Here's the code i use for zooming:

- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer {
if (self.minimumZoomScale == self.maximumZoomScale) return;
if (self.zoomScale == self.minimumZoomScale) didDoubleTap = NO;

CGPoint pointInView = [recognizer locationInView:_zoomView];

CGFloat newZoomScale;
if (!didDoubleTap && self.zoomScale <= self.minimumZoomScale) {
    newZoomScale = self.zoomScale * 2.5f;
    newZoomScale = MIN(newZoomScale, self.maximumZoomScale);
    didDoubleTap = YES;
}
else {
    newZoomScale = self.minimumZoomScale;
    didDoubleTap = NO;
}

CGSize scrollViewSize = self.bounds.size;

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

CGRect rectToZoomTo = CGRectMake(x, y, w, h);
[self zoomToRect:rectToZoomTo animated:YES];
}

This works when i run it on xcode 5.1 perfectly, like in any other photo apps. But when i run / build it on xcode 6 gm the animation for the zoom gets wierd. First the content offset is set to the future location, and then it zooms in. So the images is jumping up or down.

anyone had this issue before or have any ideas?

Tore Olsen
  • 2,153
  • 2
  • 22
  • 35
Kevin Flachsmann
  • 466
  • 1
  • 4
  • 17

2 Answers2

10

okay i found a workaround. the problem is the animation calls layoutsubviews before the zooming. so now i do my own animation

[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^ {
    inTransition = YES;
    [self zoomToRect:rectToZoomTo animated:NO];
    inTransition = NO;
    [self layoutSubviews];
} completion:nil];

but it would be nice if anyone could tell me why i have to do that.

EDIT: If someone's still interested, move the code from layoutsubviews toscrollViewDidZoom:(UIScrollView *)scrollView. So there's no need for that weird animation an pinch to zoom also works smoothly.

Kevin Flachsmann
  • 466
  • 1
  • 4
  • 17
4

I found the same bug when zooming with pinch gesture. I solved this by calling layoutSubviews in scrollViewDidScroll delegate method:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self layoutSubviews];
}
bartl
  • 392
  • 2
  • 11
  • 2
    I just juse this in layoutSubviews `if (self.zoomScale < self.minimumZoomScale) return;` – Kevin Flachsmann Sep 23 '14 at 07:39
  • Thanks! Calling `[myScrollView layoutSubviews]` in `- (void)scrollViewDidZoom:(UIScrollView *)scrollView;` when using `[myScrollView setZoomScale:myScale animated:YES];` fixed it for me in an old app. – Benjamin Bojko Apr 29 '15 at 16:02