Search code examples
iphonecocoa-touchuiviewuiviewcontrollercore-animation

How can I make my view shrink and disappear?


When my user presses a button, I want the visible view to shrink and disappear, revealing the parent view underneath. Currently I have this, which almost works:

-(void)deleteNoteTransition
{
    self.view.userInteractionEnabled=NO;
    [UIView beginAnimations:@"deleteNote" context:nil];
    [UIView setAnimationDuration:0.6];
    self.view.transform=CGAffineTransformMakeScale(0.01,0.01);
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:)];
    [UIView commitAnimations];
}

- (void)animationDidStop:(NSString *)anim finished:(BOOL)flag
{
    self.view.userInteractionEnabled=YES;
    if ([anim isEqualToString:@"deleteNote"]) {
    [self.navigationController popViewControllerAnimated:YES];
    }
}

There are two problems with this, however:

  • The view that does the shrinking is not the "whole" view - it does not include the title bar and toolbar.
  • The view shrinks to reveal a plain white background, and only then is the second method triggered, so the white background is unceremoniously replaced with the parent view.

Is there a way to include the whole screen and smoothly reveal the parent view controller behind, without getting into OpenGL stuff which is far too complicated for my current level of programming?


Solution

  • This should capture your whole view:

    #import <QuartzCore/QuartzCore.h>
    UIGraphicsBeginImageContext(viewToCapture.bounds.size);
    [viewToCapture.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    Once your capture your view to an image, use this method to scale and fade the image:

    UIImageView *firstView = nil;
    
    -(void)animateOut:(UIImage*)image {
        firstView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
        firstView.image = image
        [window addSubview:firstView];
        [window bringSubviewToFront:firstView];
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0];
        [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:window cache:YES];
        [UIView setAnimationDelegate:self]; 
        [UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
        firstView.alpha = 0.0;
        // change this for a different scale
        firstView.frame = CGRectMake(-60, -85, 440, 635);  
        [UIView commitAnimations];
    }
    
    - (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
        [firstView removeFromSuperview];
        [firstView release];
    }
    

    You can get the window from the [UIApplication sharedApplication], or just add it as a subview of the view of the current view controller.