自定义UINavigationController 切换动画

来源:互联网 发布:js new做了什么 编辑:程序博客网 时间:2024/06/05 20:56

//

//  NavControllerDelegate.h

//  WZKPonyBiJi

//

//  Created by 王正魁 on 15-3-17.

//  Copyright (c) 2015 psylife. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "Animator.h"

#import "Animations.h"

#import <UIKit/UIKit.h>

typedef enum

{

    //以下是枚举成员

    TestA = 0,//缩小渐变

    TestB,//下退渐变

    

    

}animationType;

@interface NavControllerDelegate :NSObject <UINavigationControllerDelegate,UIViewControllerTransitioningDelegate>

+ (NavControllerDelegate *) sharedInstance;

@property (strong,nonatomic) IBOutletUINavigationController *navigationController;

@property(nonatomic,strong)UIPinchGestureRecognizer *pinchRecognizer;

@property (nonatomic,assign) animationType animationForTransition;

-(void)addtag;

@end

/*

 NavControllerDelegate* nav = [NavControllerDelegate sharedInstance];//初始化导航控制器动画

 MainViewController* main = [[MainViewController alloc] init];

 nav.navigationController = [[UINavigationController alloc] initWithRootViewController:main];//常见有动画的导航控制器

 [nav addtag];//将动画和手势加到导航控制器上

 */




//------------------------------------------------------




//

//  NavControllerDelegate.m

//  WZKPonyBiJi

//

//  Created by 王正魁 on 15-3-17.

//  Copyright (c) 2015 psylife. All rights reserved.

//


#import "NavControllerDelegate.h"


@interface NavControllerDelegate ()

{

    CGFloat _startScale;

}

@property (strong,nonatomic) Animator* animator;

@property (strong,nonatomic) Animations* animatorBack;

@property (strong,nonatomic) UIPercentDrivenInteractiveTransition* interactionController;


@end

@implementation NavControllerDelegate

//将当前对象设置成单例模式

static NavControllerDelegate * sharedSingleton =nil;


+ (NavControllerDelegate *) sharedInstance

{

    if (sharedSingleton ==nil) {

        sharedSingleton = [[NavControllerDelegatealloc] init];

      

    }

    returnsharedSingleton;

}

//添加手势

-(void)addtag

{

    UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(pan:)];

    [self.navigationController.viewaddGestureRecognizer:panRecognizer];

    

    _pinchRecognizer = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(handlePinch:)];

    

    [self.navigationController.viewaddGestureRecognizer:_pinchRecognizer];

    

    

    self.navigationController.delegate =self;

    self.animator = [Animatornew];//穿件动画对象

    self.animatorBack = [Animationsnew];

}

/*

 UIPercentDrivenInteractiveTransition---

 手势和动画结合的控制类,

 现在我们可以识别该手势了,我们用它来设置并更新一个 iOS 7新加入的类的对象。 UIPercentDrivenInteractiveTransition。这个类的对象会根据我们的手势,来决定我们的自定义过渡的完成度。我们把这些都放到手势识别器的 action方法中去,具体就是:

 

 当手势刚刚开始,我们创建一个 UIPercentDrivenInteractiveTransition对象,然后让 navigationController去把当前这个 viewController弹出。

 

 当手慢慢划入时,我们把总体手势划入的进度告诉 UIPercentDrivenInteractiveTransition对象。

 

 当手势结束,我们根据用户的手势进度来判断过渡是应该完成还是取消并相应的调用 finishInteractiveTransition或者 cancelInteractiveTransition方法.

 */

-(void)handlePinch:(UIPinchGestureRecognizer *)pinch {

    CGFloat scale = pinch.scale;

    switch (pinch.state) {

        caseUIGestureRecognizerStateBegan: {

            self.interactionController = [UIPercentDrivenInteractiveTransitionnew];

            _startScale = scale;

            //            [self.interactionController isInteractive];

            if (self.navigationController.viewControllers.count >1) {

                [self.navigationControllerpopViewControllerAnimated:YES];

            }

            

            break;

        }

        caseUIGestureRecognizerStateChanged: {

            CGFloat percent = (1.0 - scale/_startScale);

            [self.interactionControllerupdateInteractiveTransition:(percent < 0.0) ?

             0.0 : percent];

            break;

        }

        caseUIGestureRecognizerStateEnded: {

            CGFloat percent = (1.0 - scale/_startScale);

            BOOL cancelled = ([pinch velocity] < 5.0 && percent

                              <= 0.3);

            if (cancelled)

            {

                [self.interactionControllercancelInteractiveTransition];

            }

            else

            {

                [self.interactionControllerfinishInteractiveTransition];

            }

            self.interactionController =nil;

            break;

        }

        caseUIGestureRecognizerStateCancelled: {

            CGFloat percent = (1.0 - scale/_startScale);

            BOOL cancelled = ([pinch velocity] < 5.0 && percent

                              <= 0.3);

            if (cancelled) {

                [self.interactionControllercancelInteractiveTransition];

            }

            else{

                [self.interactionControllerfinishInteractiveTransition];

            }

            self.interactionController =nil;

            break;

        }

    }

}


- (void)pan:(UIPanGestureRecognizer*)recognizer

{

    UIView* view =self.navigationController.view;

    if (recognizer.state ==UIGestureRecognizerStateBegan) {

        CGPoint location = [recognizer locationInView:view];

        if (location.xCGRectGetMidX(view.bounds) &&self.navigationController.viewControllers.count >1) { // left half

            self.interactionController = [UIPercentDrivenInteractiveTransitionnew];

            [self.navigationControllerpopViewControllerAnimated:YES];

        }

        else if (location.xCGRectGetMaxX(view.bounds) &&self.navigationController.viewControllers.count >1)

        {

            self.interactionController = [UIPercentDrivenInteractiveTransitionnew];

            [self.navigationControllerpopViewControllerAnimated:YES];

        }

    } else

        if (recognizer.state ==UIGestureRecognizerStateChanged) {

        CGPoint translation = [recognizer translationInView:view];

        CGFloat d = fabs(translation.x / CGRectGetWidth(view.bounds));

        [self.interactionControllerupdateInteractiveTransition:d];

    } else

        if (recognizer.state ==UIGestureRecognizerStateEnded) {

        if ([recognizer velocityInView:view].x > 0) {

            [self.interactionControllerfinishInteractiveTransition];

        } else {

            [self.interactionControllercancelInteractiveTransition];

        }

        self.interactionController =nil;

    }

}

//pushpop时调用的代理方法

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC

{

    if (operation ==UINavigationControllerOperationPop) {

        

//        [self selectAnimation];

        return self.animator;

    }

    if (operation ==UINavigationControllerOperationPush) {

//        [self selectAnimation];

        returnself.animatorBack;

    }

    return nil;

}

//UINavigationController交互动画,返回

- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController

{

    returnself.interactionController;

}

-(id<UIViewControllerAnimatedTransitioning>)selectAnimation

{

    switch (self.animationForTransition) {

        case TestA:

            return self.animator;

            break;

        case TestB:

            return self.animatorBack;

            break;

            

        default:

            return nil;

            break;

    }

}


/*

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source

{

    return self.animator;

}


- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed

{

    return self.animatorBack;

}

*/


@end


0 0