views:

697

answers:

1

I have a UIScrollView. I'd like to do some animation with it, but I want to add a neat little animation that basically zooms the UIScrollView back to where it started and then I can do some UIView animation. Is that possible?

I have tried to have a button trigger one method with the scrollView.zoomToRect, and this method calls another method in a background thread that does UIView animation. The issue is that whatever I do, the UIScrollView will just not zoom out to normal if I have an animation after it. I just want to make it zoom out followed by some animation, but I cannot.

It does not help to insert the scrollView.zoomToRect method in the animation block.

Anyone have an idea?

A: 

I am not sure if this qualifies as an answer to my own question, but in case anyone else was wondering, or in case anyone else have a better solution. I used the following code:

(Called when I hit the flip button)

- (void) flipCurrentViewZoomOut {
     // If either view is zoomed in
     if (view1.scrollView.zoomScale != 1 || view2.scrollView.zoomScale != 1 ) {
          if (view1IsVisible == YES) {
              [view1.scrollView zoomToRect:[UIScreen mainScreen].bounds animated:YES];
          } else {
              [bview2.scrollView zoomToRect:[UIScreen mainScreen].bounds animated:YES];
          }
          [UIView beginAnimations:nil context:nil];
          // The duration is enough time for the zoom-out to happen before the second animation methods gets called (flipCurrentView). 
          [UIView setAnimationDuration:0.4];
          [UIView setAnimationDelegate:self];
          // When done, then do the actual flipping of the views (exchange subviews, etc.)
          [UIView setAnimationDidStopSelector:@selector(flipCurrentView)];
          // In order for the zoomToRect to run at all, I need to do some animation, so I basically just move the view 0.01 which is not noticable (and it's animating a flip right after anyway)
          if (view1IsVisible == YES) {
              view1.frame = CGRectMake(-0.01, 0, 320, 480);
          } else {
              view2.frame = CGRectMake(-0.01, 0, 320, 480);
          }
          [UIView commitAnimations];
     } else {
          // If either view hasn't been zoomed, the flip animation is called immediately
          [self flipCurrentView];
     }
}

An important thing to note is that in my flipCurrentView method (the second animation method that flips the views), I reset the frames for view1 and view2 to [UIScreen mainScreen].bounds (in this case that's the bounds I need). I have to do this, otherwise the animation code I pasted above won't run a second time, because the origin.x will then be -0.01 and it can't animate from -0.01 to -0.01, so it would have just skip that animation block.

Let me know if I am doing something completely wrong and there's a better way to do it. Always happy to learn :)

Canada Dev