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文件下是五种动画的实现类,你需要那种动画就需要把那种动画头文件包含进去
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { Switch3DTransition *t1 = [[[Switch3DTransition alloc] init] autorelease]; t1.transitionType = Switch3DTransitionLeft; FlipTransition *t2 = [[[FlipTransition alloc] init] autorelease]; t2.transitionType = FlipTransitionRight; transitionsArray = [[NSArray alloc] initWithObjects: [[[Switch3DTransition alloc] init] autorelease], t1,[[[ClothTransition alloc] init] autorelease], [[[FlipTransition alloc] init] autorelease], t2, [[[RotateTransition alloc] init] autorelease], [[[DoorsTransition alloc] init] autorelease], nil]; transitionsNamesArray = [[NSArray alloc] initWithObjects: @"Switch 3D right", @"Switch 3D left", @"Cloth", @"Flip left", @"Flip right", @"Rotate", @"Doors", nil]; self.transition = [transitionsArray objectAtIndex:0]; } return self; }
初始化视图,并把这五种动画效果存放在 transitionsArray数组之中,Switch3DTransition默认向右,FlipTransition默认向左,分别定义了一个t1对象和t2对象,设置t1.transitionType = Switch3DTransitionLeft; t2.transitionType = FlipTransitionRight; 所以transitionsArray存放的是7种效果,对应transitionsNamesArray数组中关于动画其中效果的名字,显示在视图上的UITableViewCell上;两个数组是一一对应的关系;
两个UIView之间的动画过渡切换实现方法
//从View1切换到View2 - (void)switchToView2 { UIView *containerView = view1.superview; [[HMGLTransitionManager sharedTransitionManager] setTransition:transition]; [[HMGLTransitionManager sharedTransitionManager] beginTransition:containerView]; // Here you can do whatever you want except changing position, size or transformation of container view, or removing it from view hierarchy. view2.frame = view1.frame; [view1 removeFromSuperview]; [containerView addSubview:view2]; [[HMGLTransitionManager sharedTransitionManager] commitTransition]; }
//从View2切换到View1 - (void)switchToView1 { UIView *containerView = view2.superview; // Set transition [[HMGLTransitionManager sharedTransitionManager] setTransition:transition]; [[HMGLTransitionManager sharedTransitionManager] beginTransition:containerView]; // Here you can do whatever you want except changing position, size or transformation of container view, or removing it from view hierarchy. view1.frame = view2.frame; [view2 removeFromSuperview]; [containerView addSubview:view1]; // Commit transition [[HMGLTransitionManager sharedTransitionManager] commitTransition]; }
#pragma mark - #pragma mark ModalController delegate - (void)modalControllerDidFinish:(ModalViewController *)modalController { [[HMGLTransitionManager sharedTransitionManager] setTransition:transition]; [[HMGLTransitionManager sharedTransitionManager] dismissModalViewController:modalController]; }
ModalviewController类中定义一个ModalControllerDelegate协议,定义协议方法- (void)modalControllerDidFinish:(ModalViewController*)modalController;实现两个View之间的传值,也就是当我们在UITableViewCell对象上现则哪中过渡效果是的时候,传递HMGLTransition对象transition;
[HMGLTransitionManager sharedTransitionManager]使用了单例设计模式
实现两个UIViewController之间的动画切换方法
- (IBAction)viewTransitionButtonPressed:(id)sender { UIButton *button = (UIButton*)sender; // view transition to view1 or view2 depending on actual view if (button.superview == view1) { [self switchToView2]; } else { [self switchToView1]; } } - (IBAction)modalPresentationButtonPressed:(id)sender { [[HMGLTransitionManager sharedTransitionManager] setTransition:transition]; ModalViewController *newController; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { newController = [[ModalViewController alloc] initWithNibName:@"ModalViewController-iPad" bundle:nil]; } else { newController = [[ModalViewController alloc] initWithNibName:@"ModalViewController" bundle:nil]; } newController.delegate = self; [[HMGLTransitionManager sharedTransitionManager] presentModalViewController:newController onViewController:self]; [newController release]; }
演示一个Demo:
1.新建一个Single View Application模板工程,命名RollingView,将下载下来的工程中里HMGLTransitions文件夹拷贝加入到你的工程目录中,然后添加 QuartzCore.framework 和 OpenGLES.framework 库
2. File->New->File 添加一个控制器类ViewController2,在ViewController.h中包含头文件
// ViewController.h #import <UIKit/UIKit.h> #import "Switch3DTransition.h" #import "FlipTransition.h" #import "RotateTransition.h" #import "ClothTransition.h" #import "DoorsTransition.h" #import "ViewController2.h" #import "HMGLTransitionManager.h" @interface ViewController : UIViewController { UIButton *startBtn; HMGLTransition *transition; } @end
在ViewController.m中
自定义Button// custom Button - (UIButton *)buttonWithFrame:(CGRect)frame withNormalTitle:(NSString *)title withOtherStateTitle:(NSString *)otherTitle action:(SEL)action { UIImage *buttonBackgroundImage = [[UIImage imageNamed:@"button_background.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5]; UIImage *disabledButtonBackgroundImage = [[UIImage imageNamed:@"button_background_disabled.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = frame; [button setTitle:title forState:UIControlStateNormal]; [button setTitle:otherTitle forState:UIControlStateDisabled]; [button setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal]; [button setBackgroundImage:disabledButtonBackgroundImage forState:UIControlStateDisabled]; [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled]; [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; return button; }
- (void)viewDidLoad { [super viewDidLoad]; CGRect butRect = CGRectMake(self.view.bounds.origin.x + 60, self.view.bounds.origin.y + 100, self.view.frame.size.width-60-60, 100); startBtn= [self buttonWithFrame:butRect withNormalTitle:@"View 1" withOtherStateTitle:@"View 1" action:@selector(startView:)]; Switch3DTransition *tran = [[[Switch3DTransition alloc] init] autorelease]; tran.transitionType = Switch3DTransitionLeft; transitionArr = [[NSArray alloc] initWithObjects:[[[DoorsTransition alloc] init] autorelease], nil]; self.transition = [transitionArr objectAtIndex:0]; [HMGLTransitionManager sharedTransitionManager]; }
-(void)startView:(id)sender { [[HMGLTransitionManager sharedTransitionManager] setTransition:transition]; ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil]; [[HMGLTransitionManager sharedTransitionManager] presentModalViewController:vc2 onViewController:self]; }
在ViewController2类中,方法实现基本类似
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { FlipTransition *tran = [[[FlipTransition alloc] init] autorelease]; tran.transitionType = FlipTransitionLeft; transitionArr = [[NSArray alloc] initWithObjects:[[[FlipTransition alloc] init] autorelease], nil]; self.transition = [transitionArr objectAtIndex:0]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; CGRect butRect; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { butRect = CGRectMake(self.view.bounds.origin.x + 60, self.view.bounds.origin.y + 100, 768-60-60, 100); } else { butRect = CGRectMake(self.view.bounds.origin.x + 60, self.view.bounds.origin.y + 100, self.view.frame.size.width-60-60, 100); } endBtn= [self buttonWithFrame:butRect withNormalTitle:@"View 2" withOtherStateTitle:@"View 2" action:@selector(endView:)]; } -(void)endView:(id)sender { [[HMGLTransitionManager sharedTransitionManager] setTransition:transition]; ViewController *vc1; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { vc1 = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil]; } else { vc1 = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil]; } [[HMGLTransitionManager sharedTransitionManager] presentModalViewController:vc1 onViewController:self]; }
源码:http://download.csdn.net/detail/duxinfeng2010/5212826
欢迎转载分享,请注明出处http://blog.csdn.net/duxinfeng2010
转载于:https://blog.51cto.com/duxinfeng/1208673