HMGLTransitions 是一套动画演示两个UIView 或 UIViewController之间切换时的过渡效果;

GitHub下载地址:https://github.com/Split82/HMGLTransitions

 

有些情况下我们需要两个视图之间做一个动画过渡的切换,或许系统自带的CATransition和普通动画难以满足我们的需求,此时第三方类库就是一个不错的选择;HMGLTransitions提供五种不错效果,分别是: 3D Right(letf) 、Cloth、Flip right(letf)、Rotate和Doors

iOS第三方开源类库 -- 视图切换 HMGLTransitions_动画    iOS第三方开源类库 -- 视图切换 HMGLTransitions_开源_02  

 

iOS第三方开源类库 -- 视图切换 HMGLTransitions_第三方_03  iOS第三方开源类库 -- 视图切换 HMGLTransitions_开源_04

iOS第三方开源类库 -- 视图切换 HMGLTransitions_动画_05

 

以上是GitHub上下载自带的Demo展示的五种效果图,展示了两个UIView  和 两个UIViewController各自之间动画切换(截图中仅展示两个view之间切换),工程目录结构:

iOS第三方开源类库 -- 视图切换 HMGLTransitions_动画_06

HMGLTransitions目录下是这个第三方类库所有文件,Transitions文件下是五种动画的实现类,你需要那种动画就需要把那种动画头文件包含进去


  1. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
  2.     if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { 
  3.          
  4.         Switch3DTransition *t1 = [[[Switch3DTransition alloc] init] autorelease]; 
  5.         t1.transitionType = Switch3DTransitionLeft; 
  6.          
  7.         FlipTransition *t2 = [[[FlipTransition alloc] init] autorelease]; 
  8.         t2.transitionType = FlipTransitionRight;         
  9.          
  10.         transitionsArray = [[NSArray alloc] initWithObjects: 
  11.                             [[[Switch3DTransition alloc] init] autorelease], 
  12.                             t1,[[[ClothTransition alloc] init] autorelease],                             
  13.                             [[[FlipTransition alloc] init] autorelease], 
  14.                             t2, 
  15.                             [[[RotateTransition alloc] init] autorelease], 
  16.                             [[[DoorsTransition alloc] init] autorelease], 
  17.                             nil]; 
  18.          
  19.         transitionsNamesArray = [[NSArray alloc] initWithObjects: 
  20.                                  @"Switch 3D right"
  21.                                  @"Switch 3D left"
  22.                                  @"Cloth"
  23.                                  @"Flip left"
  24.                                  @"Flip right"
  25.                                  @"Rotate"
  26.                                  @"Doors"
  27.                                  nil]; 
  28.          
  29.          
  30.         self.transition = [transitionsArray objectAtIndex:0]; 
  31.          
  32.     } 
  33.     return self; 

 

初始化视图,并把这五种动画效果存放在 transitionsArray数组之中,Switch3DTransition默认向右,FlipTransition默认向左,分别定义了一个t1对象和t2对象,设置t1.transitionType = Switch3DTransitionLeft;   t2.transitionType = FlipTransitionRight; 所以transitionsArray存放的是7种效果,对应transitionsNamesArray数组中关于动画其中效果的名字,显示在视图上的UITableViewCell上;两个数组是一一对应的关系;

iOS第三方开源类库 -- 视图切换 HMGLTransitions_开源_07

 

 

两个UIView之间的动画过渡切换实现方法

 

  1. //从View1切换到View2 
  2.  
  3. - (void)switchToView2 { 
  4.      
  5.     UIView *containerView = view1.superview; 
  6.      
  7.     [[HMGLTransitionManager sharedTransitionManager] setTransition:transition];  
  8.     [[HMGLTransitionManager sharedTransitionManager] beginTransition:containerView]; 
  9.      
  10.     // Here you can do whatever you want except changing position, size or transformation of container view, or removing it from view hierarchy. 
  11.     view2.frame = view1.frame; 
  12.     [view1 removeFromSuperview]; 
  13.     [containerView addSubview:view2]; 
  14.      
  15.     [[HMGLTransitionManager sharedTransitionManager] commitTransition]; 
  16.  

 

  1. //从View2切换到View1 
  2.  
  3. - (void)switchToView1 { 
  4.      
  5.     UIView *containerView = view2.superview;     
  6.      
  7.     // Set transition 
  8.     [[HMGLTransitionManager sharedTransitionManager] setTransition:transition];  
  9.     [[HMGLTransitionManager sharedTransitionManager] beginTransition:containerView]; 
  10.      
  11.     // Here you can do whatever you want except changing position, size or transformation of container view, or removing it from view hierarchy. 
  12.     view1.frame = view2.frame; 
  13.     [view2 removeFromSuperview];     
  14.     [containerView addSubview:view1]; 
  15.      
  16.     // Commit transition 
  17.     [[HMGLTransitionManager sharedTransitionManager] commitTransition]; 

 

 

 

  1. - (void)modalControllerDidFinish:(ModalViewController *)modalController { 
  2.      
  3.     [[HMGLTransitionManager sharedTransitionManager] setTransition:transition];      
  4.     [[HMGLTransitionManager sharedTransitionManager] dismissModalViewController:modalController]; 

ModalviewController类中定义一个ModalControllerDelegate协议,定义协议方法- (void)modalControllerDidFinish:(ModalViewController*)modalController;实现两个View之间的传值,也就是当我们在UITableViewCell对象上现则哪中过渡效果是的时候,传递HMGLTransition对象transition;

[HMGLTransitionManager sharedTransitionManager]使用了单例设计模式

 

 

实现两个UIViewController之间的动画切换方法

 

  1. - (IBAction)viewTransitionButtonPressed:(id)sender { 
  2.     UIButton *button = (UIButton*)sender; 
  3.      
  4.     // view transition to view1 or view2 depending on actual view 
  5.     if (button.superview == view1) { 
  6.         [self switchToView2]; 
  7.     } 
  8.     else { 
  9.         [self switchToView1]; 
  10.     } 
  11.  
  12. - (IBAction)modalPresentationButtonPressed:(id)sender { 
  13.      
  14.     [[HMGLTransitionManager sharedTransitionManager] setTransition:transition];  
  15.      
  16.     ModalViewController *newController; 
  17.     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
  18.         newController = [[ModalViewController alloc] initWithNibName:@"ModalViewController-iPad" bundle:nil]; 
  19.     } 
  20.     else { 
  21.         newController = [[ModalViewController alloc] initWithNibName:@"ModalViewController" bundle:nil]; 
  22.     } 
  23.     newController.delegate = self; 
  24.      
  25.     [[HMGLTransitionManager sharedTransitionManager] presentModalViewController:newController onViewController:self]; 
  26.      
  27.     [newController release]; 

 

 

演示一个Demo:

iOS第三方开源类库 -- 视图切换 HMGLTransitions_动画_08   iOS第三方开源类库 -- 视图切换 HMGLTransitions_第三方_09

1.新建一个Single View Application模板工程,命名RollingView,将下载下来的工程中里HMGLTransitions文件夹拷贝加入到你的工程目录中,然后添加 QuartzCore.framework 和 OpenGLES.framework 库

2. File->New->File 添加一个控制器类ViewController2,在ViewController.h中包含头文件

 

  1. //  ViewController.h 
  2. #import <UIKit/UIKit.h> 
  3.  
  4. #import "Switch3DTransition.h" 
  5. #import "FlipTransition.h" 
  6. #import "RotateTransition.h" 
  7. #import "ClothTransition.h" 
  8. #import "DoorsTransition.h" 
  9.  
  10. #import "ViewController2.h" 
  11.  
  12. #import "HMGLTransitionManager.h" 
  13.  
  14. @interface ViewController : UIViewController 
  15.     UIButton *startBtn; 
  16.     HMGLTransition *transition; 
  17.  
  18.  
  19. @end 

 

在ViewController.m中

自定义Button

  1. - (UIButton *)buttonWithFrame:(CGRect)frame  withNormalTitle:(NSString *)title  withOtherStateTitle:(NSString *)otherTitle action:(SEL)action  
  2.     UIImage *buttonBackgroundImage = [[UIImage p_w_picpathNamed:@"button_background.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5]; 
  3.     UIImage *disabledButtonBackgroundImage = [[UIImage p_w_picpathNamed:@"button_background_disabled.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5]; 
  4.      
  5.     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
  6.     button.frame = frame; 
  7.     [button setTitle:title forState:UIControlStateNormal]; 
  8.     [button setTitle:otherTitle forState:UIControlStateDisabled]; 
  9.     [button setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal]; 
  10.     [button setBackgroundImage:disabledButtonBackgroundImage forState:UIControlStateDisabled]; 
  11.     [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
  12.     [button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled]; 
  13.     [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; 
  14.     [self.view addSubview:button]; 
  15.      
  16.     return button; 


 

  1. - (void)viewDidLoad 
  2.     [super viewDidLoad]; 
  3.      
  4.     CGRect butRect = CGRectMake(self.view.bounds.origin.x + 60, self.view.bounds.origin.y + 100, self.view.frame.size.width-60-60, 100); 
  5.     startBtn= [self buttonWithFrame:butRect withNormalTitle:@"View 1" withOtherStateTitle:@"View 1" action:@selector(startView:)]; 
  6.      
  7.     Switch3DTransition *tran = [[[Switch3DTransition alloc] init] autorelease]; 
  8.     tran.transitionType = Switch3DTransitionLeft; 
  9.     transitionArr = [[NSArray alloc] initWithObjects:[[[DoorsTransition alloc] init] autorelease], nil]; 
  10.     self.transition = [transitionArr objectAtIndex:0]; 
  11.      
  12.      
  13.     [HMGLTransitionManager sharedTransitionManager]; 
  14.      
点击Button实现两个UIViewController之间的动画切换

  1. -(void)startView:(id)sender 
  2.      
  3.      
  4.     [[HMGLTransitionManager sharedTransitionManager] setTransition:transition]; 
  5.     ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil]; 
  6.     [[HMGLTransitionManager sharedTransitionManager] presentModalViewController:vc2 onViewController:self]; 



在ViewController2类中,方法实现基本类似

 

  1. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
  2.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
  3.     if (self) { 
  4.         FlipTransition *tran = [[[FlipTransition alloc] init] autorelease]; 
  5.         tran.transitionType = FlipTransitionLeft; 
  6.         transitionArr = [[NSArray alloc] initWithObjects:[[[FlipTransition alloc] init] autorelease], nil]; 
  7.         self.transition = [transitionArr objectAtIndex:0]; 
  8.     } 
  9.     return self; 
  10.  

 

  1. - (void)viewDidLoad 
  2.     [super viewDidLoad]; 
  3.      
  4.     CGRect butRect; 
  5.     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
  6.         butRect = CGRectMake(self.view.bounds.origin.x + 60, self.view.bounds.origin.y + 100, 768-60-60, 100); 
  7.     } 
  8.     else { 
  9.         butRect = CGRectMake(self.view.bounds.origin.x + 60, self.view.bounds.origin.y + 100, self.view.frame.size.width-60-60, 100); 
  10.     } 
  11.      
  12.     endBtn= [self buttonWithFrame:butRect withNormalTitle:@"View 2" withOtherStateTitle:@"View 2" action:@selector(endView:)]; 
  13.  

 

  1. -(void)endView:(id)sender 
  2.     [[HMGLTransitionManager sharedTransitionManager] setTransition:transition]; 
  3.     ViewController *vc1; 
  4.     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
  5.         vc1 = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil]; 
  6.          
  7.     } 
  8.     else { 
  9.         vc1 = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil]; 
  10.     } 
  11.      
  12.     [[HMGLTransitionManager sharedTransitionManager] presentModalViewController:vc1 onViewController:self];