自定义UINavigationController的push和pop切换界面动画

方法一:

  利用iOS提供的4种视图转换类型:

  • UIViewAnimationTransitionFlipFromLeft
  • UIViewAnimationTransitionFlipFromRight
  • UIViewAnimationTransitionCurlUp
  • UIViewAnimationTransitionCurlDown

 

1 MainView *nextView=[[MainView alloc] init];  
2 [UIView  beginAnimations:nil context:NULL];  
3 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
4 [UIView setAnimationDuration:0.75];  
5 [self.navigationController pushViewController:nextView animated:NO];  
6 //选择动画
7 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO]; 
8 [UIView commitAnimations];  
9 [nextView release]; 

 

 

方法二:

  自定义动画:

  1. 在frameworks中添加QuartzCore.framework
  2. 在.m文件中导入 #import <QuartzCore/QuartzCore.h>

  

 1 CATransition* transition = [CATransition animation]; 
 2 //执行时间长短 
 3 transition.duration = 0.5;  
 4 //动画的开始与结束的快慢
 5 transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  
 6 //各种动画效果
 7 transition.type = kCATransitionFade; //kCATransitionMoveIn, kCATransitionPush, kCATransitionReveal, kCATransitionFade 
 8 //动画方向 
 9 //transition.subtype = kCATransitionFromTop; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom  
10 //将动画添加在视图层上
11 [self.navigationController.view.layer addAnimation:transition forKey:nil];  
12 [[self navigationController] popViewControllerAnimated:NO];  

 

  实际上CATransition类中还有一个属性是removedOnCompletion,是此动画执行完后会自动remove,默认值为true。

  transition的type可以返回四种类型:

  1. kCATransitionFade  淡出
  2. kCATransitionMoveIn  覆盖原图
  3. kCATransitionPush     推出
  4. kCATransitionReveal   底部显出来

  transition的subtype也可以有四种类型:

  1. kCATransitionFromRight
  2. kCATransitionFromLeft   (默认值)
  3. kCATransitionFromTop
  4. kCATransitionFromBottom

  还有一种设置动画类型的方法,不用setSubtype,只用setType

  [transition setType:@"suckEffect"];   或者  transition.type = @"suckEffect";

  这里的suckEffect为效果名,主要效果有:

  • pageCurl      向上翻一页
  • pageUnCurl  向下翻一页
  • rippleEffect   滴水效果
  • suckEffect    收缩效果,如一块布被抽走
  • cube            立方体效果
  • oglFlip         上下翻转效果