Search code examples
ioscore-animation

How do I perform these animated view transitions?


I am brand new to Core Animation, and I need to know how to do 2 animations:

  1. I need to switch XIBs by fading through black (fully releasing the the first view controller)
  2. I need to mimic the UINavigationController's pushViewController animation (switching XIBs and releasing the first view controller)

How can you achieve these animated view transitions?


Solution

  • I've done both of these animations, but maybe not in the exact way you are looking for.

    1. Fade View to black, I took this the other way an instead added a new subview that covered the entire window that was Black and animated the Alpha from 0.0 to 1.0. Made for a nice effect.

      [UIView animateWithDuration:0.5 animations:^{ _easterEgg.alpha = 1.0; } completion:^(BOOL finished) { [self animateIndex:0]; }];

    2. Slide in a view like UINavigationController. I didn't do this exactly like UINavigationController since it does multiple animations, but I did have a new view slide the previous view off screen. This code sets the frame of the new view off screen to the right of the current view, builds a frame location that is off the screen to the left, and grabs the current visible frame. Finally it just animates the new view from off screen right into the visible frame, and the old view from the visible frame to off left. Then removes the old view.

      CGRect offRight = CGRectMake(_contentView.frame.size.width, 0, _contentView.frame.size.width, _contentView.frame.size.height);

      CGRect offLeft = CGRectMake(-_contentView.frame.size.width, 0, _contentView.frame.size.width, _contentView.frame.size.height);

      CGRect visibleFrame = CGRectMake(0, 0, _contentView.frame.size.width, _contentView.frame.size.height);

      [view setFrame:offRight]; UIView *currentView = [[_contentView subviews] lastObject]; [_contentView addSubview:view];

      [UIView animateWithDuration:0.5 animations:^{ [currentView setFrame:offLeft]; [view setFrame:visibleFrame]; } completion:^(BOOL finished) { [currentView removeFromSuperview]; }];