iOS7使用代理自定義導航轉場動畫

如果你已經厭倦了使用UINavigationController進行簡單粗暴的push和pop轉場操作,你完全可以使用自定義的導航轉場效果,iOS7提供了許多漂亮的代理方法幫助你實現各種自定義動畫,下面演示一個簡單的導航轉場動畫Demo的實現過程,效果如圖一所示:




STEP 1: 創建一個項目,其中包含,一個名爲FirstViewController和一個SecondViewController的視圖控制器,兩個繼承自NSObject的PopAnimation和PushAnimation類。


STEP2:使用UIViewControllerAnimatiedTransitioning代理方法自定義Push 動畫

 1.)讓PopAnimation使用UIViewControllerAnimatedTransitioning代理的方法:

@interface PushAnimation : NSObject <UIViewControllerAnimatedTransitioning>

2.)PopAnimation的實現


- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    [[transitionContext containerView] addSubview:toViewController.view];
    toViewController.view.alpha = 0;
  
    [UIView animateWithDuration:0.3*[self transitionDuration:transitionContext]  animations:^{
        fromViewController.view.transform = CGAffineTransformMakeScale(0.6, 0.6);
        toViewController.view.alpha = 0.3;
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:0.3*[self transitionDuration:transitionContext]  animations:^{
            
            fromViewController.view.transform = CGAffineTransformMakeScale(1.1, 1.1);
            toViewController.view.alpha = 0.6;

        } completion:^(BOOL finished){
            
            [UIView animateWithDuration:0.4*[self transitionDuration:transitionContext] animations:^{
                
                fromViewController.view.transform = CGAffineTransformMakeScale(0.1, 0.1);
                toViewController.view.alpha = 1.0;
            } completion:^(BOOL finished){
                
                fromViewController.view.transform = CGAffineTransformIdentity;
                [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
            }];          
        }];
    }];
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 3.0f;
}

STEP 3: 使用UIViewControllerAnimatiedTransitioning代理方法自定義Pop 動畫

@interface PopAnimation : NSObject <UIViewControllerAnimatedTransitioning>

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    
        UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    [[transitionContext containerView] addSubview:toViewController.view];
    
    toViewController.view.alpha = 0;
    toViewController.view.transform = CGAffineTransformMakeScale(0.1, 0.1);
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        toViewController.view.transform = CGAffineTransformMakeScale(1.0, 1.0);
        toViewController.view.alpha = 1.0f;
        fromViewController.view.alpha = 0.0f;
    
    } completion:^(BOOL finished) {
        fromViewController.view.transform = CGAffineTransformIdentity;
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        
    }];
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 3.0f;
}

STEP4: 在FirstViewController中實現UINavigationControllerDelegate的一些方法

#import "PushAnimation.h"
@interface FirstViewController : UIViewController <UINavigationControllerDelegate>
@property (nonatomic, strong) PushAnimation *animation;

- (void)viewDidLoad
{
    [super viewDidLoad];
    _animation = [PushAnimation new];
    
    //隱藏NavigationBar
    self.navigationController.navigationBarHidden = YES;
    
    //設置背景圖片
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
    [imageView setImage:[UIImage imageNamed:@"1.jpg"]];
    [self.view addSubview:imageView];
    
    //定義一個button
    UIButton *pushBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    pushBtn.frame = CGRectMake(284, 700, 200, 40);
    [pushBtn setTitle:@"push" forState:UIControlStateNormal];
    pushBtn.titleLabel.font = [UIFont systemFontOfSize:32.0f];
    [pushBtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:pushBtn];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.navigationController.delegate == self) {
        self.navigationController.delegate = nil;
    }
}

- (void)buttonClicked:(id)sender {
    
    SecondViewController *presentController = [[SecondViewController alloc]init];
    
    [self.navigationController pushViewController:presentController animated:YES];
}

#pragma mask UINavigationControllerDelegate

- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC {
    if (operation == UINavigationControllerOperationPush) {
        return self.animation;
    }
    return nil;
}

STEP5: 在SecondViewController中調用PopAnimation動畫


@interface SecondViewController : UIViewController <UINavigationControllerDelegate>
@property(nonatomic, strong) PopAnimation *animation;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _animation = [PopAnimation new];
    
    //隱藏NavigationBar
    self.navigationController.navigationBarHidden = YES;
    
    //設置背景圖片
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
    [imageView setImage:[UIImage imageNamed:@"2.jpg"]];
    [self.view addSubview:imageView];
    
    //定義一個button
    UIButton *pushBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    pushBtn.frame = CGRectMake(284, 700, 200, 40);
    [pushBtn setTitle:@"Pop" forState:UIControlStateNormal];
    pushBtn.titleLabel.font = [UIFont systemFontOfSize:32.0f];
    [pushBtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:pushBtn];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.navigationController.delegate == self) {
        self.navigationController.delegate = nil;
    }
}

- (void)buttonClicked:(id)sender {
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mask UINavigationControllerDelegate

- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC {
    if (operation == UINavigationControllerOperationPop) {
        return self.animation;
    }
    return nil;
}

STEP6:  Over 了


源碼下載地址: http://download.csdn.net/detail/luozhonglan/7979545

***************************************************************************************************************************************************************************************













發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章