天天看點

iOS第三方開源類庫 -- 視圖切換 HMGLTransitions

HMGLTransitions 是一套動畫示範兩個UIView 或 UIViewController之間切換時的過渡效果;

有些情況下我們需要兩個視圖之間做一個動畫過渡的切換,或許系統自帶的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 

    view1.frame = view2.frame; 

    [view2 removeFromSuperview];     

    [containerView addSubview:view1]; 

    // Commit transition 

- (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 { 

    ModalViewController *newController; 

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 

        newController = [[ModalViewController alloc] initWithNibName:@"ModalViewController-iPad" bundle:nil]; 

        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

- (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]; 

點選Button實作兩個UIViewController之間的動畫切換

-(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]; 

    CGRect butRect; 

        butRect = CGRectMake(self.view.bounds.origin.x + 60, self.view.bounds.origin.y + 100, 768-60-60, 100); 

        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 

    ViewController *vc1; 

        vc1 = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil]; 

        vc1 = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil]; 

    [[HMGLTransitionManager sharedTransitionManager] presentModalViewController:vc1 onViewController:self]; 

     本文轉自新風作浪 51CTO部落格,原文連結:http://blog.51cto.com/duxinfeng/1169146,如需轉載請自行聯系原作者

繼續閱讀