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

来源:互联网 发布:java登陆注册实现 编辑:程序博客网 时间:2024/05/22 03:39

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

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


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

      


  



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


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


[cpp] view plaincopy
  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;  
  34. }  

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




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

[cpp] view plaincopy
  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. }  
[cpp] view plaincopy
  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];  
  18. }  


[cpp] view plaincopy
  1. #pragma mark -  
  2. #pragma mark ModalController delegate  
  3.   
  4.   
  5. - (void)modalControllerDidFinish:(ModalViewController *)modalController {  
  6.   
  7.     [[HMGLTransitionManager sharedTransitionManager] setTransition:transition];       
  8.     [[HMGLTransitionManager sharedTransitionManager] dismissModalViewController:modalController];  
  9. }  

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

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


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

[cpp] view plaincopy
  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.   
  13. - (IBAction)modalPresentationButtonPressed:(id)sender {  
  14.       
  15.     [[HMGLTransitionManager sharedTransitionManager] setTransition:transition];   
  16.       
  17.     ModalViewController *newController;  
  18.     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {  
  19.         newController = [[ModalViewController alloc] initWithNibName:@"ModalViewController-iPad" bundle:nil];  
  20.     }  
  21.     else {  
  22.         newController = [[ModalViewController alloc] initWithNibName:@"ModalViewController" bundle:nil];  
  23.     }  
  24.     newController.delegate = self;  
  25.       
  26.     [[HMGLTransitionManager sharedTransitionManager] presentModalViewController:newController onViewController:self];  
  27.       
  28.     [newController release];  
  29. }  



演示一个Demo:

   

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

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

[cpp] view plaincopy
  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. {  
  16.     UIButton *startBtn;  
  17.     HMGLTransition *transition;  
  18. }  
  19.   
  20.   
  21. @end  


在ViewController.m中

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


[cpp] view plaincopy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.     CGRect butRect = CGRectMake(self.view.bounds.origin.x + 60, self.view.bounds.origin.y + 100, self.view.frame.size.width-60-60, 100);  
  6.     startBtn= [self buttonWithFrame:butRect withNormalTitle:@"View 1" withOtherStateTitle:@"View 1" action:@selector(startView:)];  
  7.       
  8.     Switch3DTransition *tran = [[[Switch3DTransition alloc] init] autorelease];  
  9.     tran.transitionType = Switch3DTransitionLeft;  
  10.     transitionArr = [[NSArray alloc] initWithObjects:[[[DoorsTransition alloc] init] autorelease], nil];  
  11.     self.transition = [transitionArr objectAtIndex:0];  
  12.   
  13.       
  14.     [HMGLTransitionManager sharedTransitionManager];  
  15.   
  16. }  


点击Button实现两个UIViewController之间的动画切换
[cpp] view plaincopy
  1. -(void)startView:(id)sender  
  2. {  
  3.       
  4.   
  5.     [[HMGLTransitionManager sharedTransitionManager] setTransition:transition];  
  6.     ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];  
  7.     [[HMGLTransitionManager sharedTransitionManager] presentModalViewController:vc2 onViewController:self];  
  8. }  


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

[cpp] view plaincopy
  1. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  2. {  
  3.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  4.     if (self) {  
  5.         FlipTransition *tran = [[[FlipTransition alloc] init] autorelease];  
  6.         tran.transitionType = FlipTransitionLeft;  
  7.         transitionArr = [[NSArray alloc] initWithObjects:[[[FlipTransition alloc] init] autorelease], nil];  
  8.         self.transition = [transitionArr objectAtIndex:0];  
  9.     }  
  10.     return self;  
  11. }  
  12.   
  13. - (void)viewDidLoad  
  14. {  
  15.     [super viewDidLoad];  
  16.       
  17.     CGRect butRect;  
  18.     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {  
  19.         butRect = CGRectMake(self.view.bounds.origin.x + 60, self.view.bounds.origin.y + 100, 768-60-60, 100);  
  20.     }  
  21.     else {  
  22.         butRect = CGRectMake(self.view.bounds.origin.x + 60, self.view.bounds.origin.y + 100, self.view.frame.size.width-60-60, 100);  
  23.     }  
  24.       
  25.     endBtn= [self buttonWithFrame:butRect withNormalTitle:@"View 2" withOtherStateTitle:@"View 2" action:@selector(endView:)];  
  26. }  
  27.   
  28. -(void)endView:(id)sender  
  29. {  
  30.     [[HMGLTransitionManager sharedTransitionManager] setTransition:transition];  
  31.     ViewController *vc1;  
  32.     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {  
  33.         vc1 = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];  
  34.           
  35.     }  
  36.     else {  
  37.         vc1 = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];  
  38.     }  
  39.        
  40.     [[HMGLTransitionManager sharedTransitionManager] presentModalViewController:vc1 onViewController:self];  
  41. }  



源码:http://download.csdn.net/detail/duxinfeng2010/521282

转自:http://blog.csdn.net/duxinfeng2010/article/details/8748145

原创粉丝点击