Change animation transition

2019-01-15 03:53发布

I got an app with NavigationController. How can i change animation transition style of pushViewController and popToViewController?

UPD

I created category like in @lawicko answer. But i got error when i am trying to call function

[self.navigationController pushViewController:places withCustomTransition:CustomViewAnimationTransitionPush subtype:CustomViewAnimationSubtypeFromLeft];

error is : "use of undeclared identifier 'CustomViewAnimationTransitionPush'"

Where should i declare this part:

typedef enum {
    CustomViewAnimationTransitionNone,
    CustomViewAnimationTransitionFlipFromLeft,
    CustomViewAnimationTransitionFlipFromRight,
    CustomViewAnimationTransitionCurlUp,
    CustomViewAnimationTransitionCurlDown,
    CustomViewAnimationTransitionFadeIn,
    CustomViewAnimationTransitionMoveIn,
    CustomViewAnimationTransitionPush,
    CustomViewAnimationTransitionReveal
} CustomViewAnimationTransition;

Write now i declare it in UINavigationController+Additions.h

UPD 2: One more new error:

Undefined symbols for architecture i386:
  "_OBJC_CLASS_$_CATransition", referenced from:
      objc-class-ref in UINavigationController+Additions.o
  "_kCATransition", referenced from:

and same errors foor all _kCATransitions

3条回答
等我变得足够好
2楼-- · 2019-01-15 03:59

Check out this UINavigationController category that I created. It allows you pushing and popping with pretty much every possible transition, and also supports subtypes for QuartzCore transitions, which will allow you to do exactly what you want - push the view from the left. Do it like this:

[self.navigationController pushViewController:[[MyController alloc] init] withCustomTransition:CustomViewAnimationTransitionPush subtype:CustomViewAnimationSubtypeFromLeft];

The code is below. The first part you need to put in the header part:

// IMPORTANT - basic transitions like flip and curl are local, they reside only in animation block. Core animations however,
// once assigned to the layer, stay until changed or reset (by assigning nil as layer animation property)

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

typedef enum {
    CustomViewAnimationTransitionNone,
    CustomViewAnimationTransitionFlipFromLeft,
    CustomViewAnimationTransitionFlipFromRight,
    CustomViewAnimationTransitionCurlUp,
    CustomViewAnimationTransitionCurlDown,
    CustomViewAnimationTransitionFadeIn,
    CustomViewAnimationTransitionMoveIn,
    CustomViewAnimationTransitionPush,
    CustomViewAnimationTransitionReveal
} CustomViewAnimationTransition;

#define CustomViewAnimationSubtypeFromRight kCATransitionFromRight
#define CustomViewAnimationSubtypeFromLeft kCATransitionFromLeft
#define CustomViewAnimationSubtypeFromTop kCATransitionFromTop
#define CustomViewAnimationSubtypeFromBottom kCATransitionFromBottom

@interface UINavigationController(Additions)

- (void)pushViewController:(UIViewController *)viewController withCustomTransition:(CustomViewAnimationTransition)transition subtype:(NSString*)subtype;

- (void)popViewControllerWithCustomTransition:(CustomViewAnimationTransition)transition subtype:(NSString*)subtype;
- (void)popToRootViewControllerWithCustomTransition:(CustomViewAnimationTransition)transition subtype:(NSString*)subtype;
- (void)popToViewController:(UIViewController *)viewController withCustomTransition:(CustomViewAnimationTransition)transition subtype:(NSString*)subtype;

@end

This second part you need to put in the implementation file:

#import "UINavigationController_Additions.h"

@interface UINavigationController()

- (void)standardAnimationWithController:(UIViewController*)viewController
                                duration:(NSTimeInterval)duration
                                options:(UIViewAnimationOptions)options
                                changesBlock:(void (^)(void))block;
- (void)coreAnimationWithController:(UIViewController*)viewController
                                duration:(NSTimeInterval)duration
                                type:(NSString*)type
                                subtype:(NSString*)subtype
                                changesBlock:(void (^)(void))block;
@end

@implementation UINavigationController(Additions)

#pragma mark -
#pragma mark pushing

- (void)pushViewController:(UIViewController *)viewController withCustomTransition:(CustomViewAnimationTransition)transition subtype:(NSString*)subtype {
    switch (transition) {
        case CustomViewAnimationTransitionNone:{
            [self standardAnimationWithController:viewController duration:.5 options:UIViewAnimationOptionTransitionNone
                                            changesBlock:^{
                                                [self pushViewController:viewController animated:NO];
                                            }];
            break;}
        case CustomViewAnimationTransitionFlipFromLeft:{
            [self standardAnimationWithController:viewController duration:.5 options:UIViewAnimationOptionTransitionFlipFromLeft
                                            changesBlock:^{
                                                [self pushViewController:viewController animated:NO];
                                            }];
            break;}
        case CustomViewAnimationTransitionFlipFromRight:{
            [self standardAnimationWithController:viewController duration:.5 options:UIViewAnimationOptionTransitionFlipFromRight
                                            changesBlock:^{
                                                [self pushViewController:viewController animated:NO];
                                            }];
            break;}
        case CustomViewAnimationTransitionCurlUp:{
            [self standardAnimationWithController:viewController duration:.5 options:UIViewAnimationOptionTransitionCurlUp
                                            changesBlock:^{
                                                [self pushViewController:viewController animated:NO];
                                            }];
            break;}
        case CustomViewAnimationTransitionCurlDown:{
            [self standardAnimationWithController:viewController duration:.5 options:UIViewAnimationOptionTransitionCurlDown
                                            changesBlock:^{
                                                [self pushViewController:viewController animated:NO];
                                            }];
            break;}
        case CustomViewAnimationTransitionFadeIn:{
            [self coreAnimationWithController:viewController duration:.5 type:kCATransitionFade subtype:nil
                                 changesBlock:^{
                                     [self pushViewController:viewController animated:NO];
                                 }];
            break;}
        case CustomViewAnimationTransitionMoveIn:{
            [self coreAnimationWithController:viewController duration:.5 type:kCATransitionMoveIn subtype:subtype
                                 changesBlock:^{
                                     [self pushViewController:viewController animated:NO];
                                 }];
            break;}
        case CustomViewAnimationTransitionPush:{
            [self coreAnimationWithController:viewController duration:.5 type:kCATransitionPush subtype:subtype
                                 changesBlock:^{
                                     [self pushViewController:viewController animated:NO];
                                 }];
            break;}
        case CustomViewAnimationTransitionReveal:{
            [self coreAnimationWithController:viewController duration:.5 type:kCATransitionReveal subtype:subtype
                                 changesBlock:^{
                                     [self pushViewController:viewController animated:NO];
                                 }];
            break;}
        default:{
            break;}
    }
}

#pragma mark -
#pragma mark popping

- (void)popViewControllerWithCustomTransition:(CustomViewAnimationTransition)transition subtype:(NSString*)subtype {
    switch (transition) {
        case CustomViewAnimationTransitionNone:{
            [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionNone
                                     changesBlock:^{
                                         [self popViewControllerAnimated:NO];
                                     }];
            break;}
        case CustomViewAnimationTransitionFlipFromLeft:{
            [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionFlipFromLeft
                                     changesBlock:^{
                                         [self popViewControllerAnimated:NO];
                                     }];
            break;}
        case CustomViewAnimationTransitionFlipFromRight:{
            [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionFlipFromRight
                                     changesBlock:^{
                                         [self popViewControllerAnimated:NO];
                                     }];
            break;}
        case CustomViewAnimationTransitionCurlUp:{
            [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionCurlUp
                                     changesBlock:^{
                                         [self popViewControllerAnimated:NO];
                                     }];
            break;}
        case CustomViewAnimationTransitionCurlDown:{
            [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionCurlDown
                                     changesBlock:^{
                                         [self popViewControllerAnimated:NO];
                                     }];
            break;}
        case CustomViewAnimationTransitionFadeIn:{
            [self coreAnimationWithController:nil duration:.5 type:kCATransitionFade subtype:nil
                                 changesBlock:^{
                                     [self popViewControllerAnimated:NO];
                                 }];
            break;}
        case CustomViewAnimationTransitionMoveIn:{
            [self coreAnimationWithController:nil duration:.5 type:kCATransitionMoveIn subtype:subtype
                                 changesBlock:^{
                                     [self popViewControllerAnimated:NO];
                                 }];
            break;}
        case CustomViewAnimationTransitionPush:{
            [self coreAnimationWithController:nil duration:.5 type:kCATransitionPush subtype:subtype
                                 changesBlock:^{
                                     [self popViewControllerAnimated:NO];
                                 }];
            break;}
        case CustomViewAnimationTransitionReveal:{
            [self coreAnimationWithController:nil duration:.5 type:kCATransitionReveal subtype:subtype
                                 changesBlock:^{
                                     [self popViewControllerAnimated:NO];
                                 }];
            break;}
        default:{
            break;}
    }
}

- (void)popToRootViewControllerWithCustomTransition:(CustomViewAnimationTransition)transition subtype:(NSString*)subtype {
    switch (transition) {
        case CustomViewAnimationTransitionNone:{
            [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionNone
                                     changesBlock:^{
                                         [self popToRootViewControllerAnimated:NO];
                                     }];
            break;}
        case CustomViewAnimationTransitionFlipFromLeft:{
            [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionFlipFromLeft
                                     changesBlock:^{
                                         [self popToRootViewControllerAnimated:NO];
                                     }];
            break;}
        case CustomViewAnimationTransitionFlipFromRight:{
            [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionFlipFromRight
                                     changesBlock:^{
                                         [self popToRootViewControllerAnimated:NO];
                                     }];
            break;}
        case CustomViewAnimationTransitionCurlUp:{
            [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionCurlUp
                                     changesBlock:^{
                                         [self popToRootViewControllerAnimated:NO];
                                     }];
            break;}
        case CustomViewAnimationTransitionCurlDown:{
            [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionCurlDown
                                     changesBlock:^{
                                         [self popToRootViewControllerAnimated:NO];
                                     }];
            break;}
        case CustomViewAnimationTransitionFadeIn:{
            [self coreAnimationWithController:nil duration:.5 type:kCATransitionFade subtype:nil
                                 changesBlock:^{
                                     [self popToRootViewControllerAnimated:NO];
                                 }];
            break;}
        case CustomViewAnimationTransitionMoveIn:{
            [self coreAnimationWithController:nil duration:.5 type:kCATransitionMoveIn subtype:subtype
                                 changesBlock:^{
                                     [self popToRootViewControllerAnimated:NO];
                                 }];
            break;}
        case CustomViewAnimationTransitionPush:{
            [self coreAnimationWithController:nil duration:.5 type:kCATransitionPush subtype:subtype
                                 changesBlock:^{
                                     [self popToRootViewControllerAnimated:NO];
                                 }];
            break;}
        case CustomViewAnimationTransitionReveal:{
            [self coreAnimationWithController:nil duration:.5 type:kCATransitionReveal subtype:subtype
                                 changesBlock:^{
                                     [self popToRootViewControllerAnimated:NO];
                                 }];
            break;}
        default:{
            break;}
    }    
}

- (void)popToViewController:(UIViewController *)viewController withCustomTransition:(CustomViewAnimationTransition)transition subtype:(NSString*)subtype {
    switch (transition) {
        case CustomViewAnimationTransitionNone:{
            [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionNone
                                     changesBlock:^{
                                         [self popToViewController:viewController animated:NO];
                                     }];
            break;}
        case CustomViewAnimationTransitionFlipFromLeft:{
            [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionFlipFromLeft
                                     changesBlock:^{
                                         [self popToViewController:viewController animated:NO];
                                     }];
            break;}
        case CustomViewAnimationTransitionFlipFromRight:{
            [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionFlipFromRight
                                     changesBlock:^{
                                         [self popToViewController:viewController animated:NO];
                                     }];
            break;}
        case CustomViewAnimationTransitionCurlUp:{
            [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionCurlUp
                                     changesBlock:^{
                                         [self popToViewController:viewController animated:NO];
                                     }];
            break;}
        case CustomViewAnimationTransitionCurlDown:{
            [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionCurlDown
                                     changesBlock:^{
                                         [self popToViewController:viewController animated:NO];
                                     }];
            break;}
        case CustomViewAnimationTransitionFadeIn:{
            [self coreAnimationWithController:nil duration:.5 type:kCATransitionFade subtype:nil
                                 changesBlock:^{
                                     [self popToViewController:viewController animated:NO];
                                 }];
            break;}
        case CustomViewAnimationTransitionMoveIn:{
            [self coreAnimationWithController:nil duration:.5 type:kCATransitionMoveIn subtype:subtype
                                 changesBlock:^{
                                     [self popToViewController:viewController animated:NO];
                                 }];
            break;}
        case CustomViewAnimationTransitionPush:{
            [self coreAnimationWithController:nil duration:.5 type:kCATransitionPush subtype:subtype
                                 changesBlock:^{
                                     [self popToViewController:viewController animated:NO];
                                 }];
            break;}
        case CustomViewAnimationTransitionReveal:{
            [self coreAnimationWithController:nil duration:.5 type:kCATransitionReveal subtype:subtype
                                 changesBlock:^{
                                     [self popToViewController:viewController animated:NO];
                                 }];
            break;}
        default:{
            break;}
    }        
}

#pragma mark -
#pragma mark private

- (void)standardAnimationWithController:(UIViewController*)viewController
                                duration:(NSTimeInterval)duration
                                options:(UIViewAnimationOptions)options
                                changesBlock:(void (^)(void))block {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];
    [UIView transitionWithView:self.view duration:duration options:options animations:block completion:NULL];
    [UIView commitAnimations];
}

- (void)coreAnimationWithController:(UIViewController*)viewController
                                duration:(NSTimeInterval)duration
                                type:(NSString*)type
                                subtype:(NSString*)subtype
                                changesBlock:(void (^)(void))block {
    CATransition* trans = [CATransition animation];
    [trans setDuration:duration];
    [trans setType:type];
    [trans setSubtype:subtype];
    [self.view.layer addAnimation:trans forKey:kCATransition];
    block();
}

@end
查看更多
手持菜刀,她持情操
3楼-- · 2019-01-15 04:01

I recently tackled creating my own transition, here's the reusable library I made:

https://github.com/travisjeffery/TRVSNavigationControllerTransition

And here's my blog post talking about how to make your own transition.

The basic idea is pretty simple though, just take a CALayer snapshot of the navigationController's (current) view, then push/pop the view off without animation, take a CALayer snapshot of the new view and then add your own animations to those layers and then remove those layers once the animation is completed.

查看更多
Emotional °昔
4楼-- · 2019-01-15 04:17

You need to add QuartzCore.framework to your target to solve _OBJC_CLASS_$_CATransition error.

查看更多
登录 后发表回答