更改动画过渡方式

4
我有一个带有NavigationController的应用程序。我如何更改pushViewControllerpopToViewController的动画过渡样式?
更新:
我像@lawicko答案中所示创建了一个类别。但是,当我尝试调用函数时,出现错误。
[self.navigationController pushViewController:places withCustomTransition:CustomViewAnimationTransitionPush subtype:CustomViewAnimationSubtypeFromLeft];
错误是:“使用未声明的标识符'CustomViewAnimationTransitionPush'”
我应该在哪里声明这个部分?
typedef enum {
    CustomViewAnimationTransitionNone,
    CustomViewAnimationTransitionFlipFromLeft,
    CustomViewAnimationTransitionFlipFromRight,
    CustomViewAnimationTransitionCurlUp,
    CustomViewAnimationTransitionCurlDown,
    CustomViewAnimationTransitionFadeIn,
    CustomViewAnimationTransitionMoveIn,
    CustomViewAnimationTransitionPush,
    CustomViewAnimationTransitionReveal
} CustomViewAnimationTransition;

现在我在UINavigationController+Additions.h中声明它。

更新2:又出现了一个新错误:

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

对于所有的_kCATransitions都出现相同的错误


你试过这个吗? - Ganzolo
没问题,但我想要像返回按钮那样的动画.. 但是要设置为pushViewController。 - Eugene Trapeznikov
我不是很明白。您希望在推送视图控制器(pushViewController)时使用点击返回按钮(popViewController)时的动画效果? - Ganzolo
是的!我想要像水平滑动一样的动画。但是方向向左。 - Eugene Trapeznikov
3个回答

19

看看我创建的UINavigationController类别。它允许您使用几乎所有可能的转换进行推送和弹出,并支持用于QuartzCore转换的子类型,这将使您能够按照所需方式进行推送视图 - 从左侧推送。操作步骤如下:

[self.navigationController pushViewController:[[MyController alloc] init] withCustomTransition:CustomViewAnimationTransitionPush subtype:CustomViewAnimationSubtypeFromLeft];
以下是代码。第一部分需要放在头部:
// 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

您需要将这第二部分放在实现文件中:

#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

干得好,它的表现真的很好。在使用CustomViewAnimationTransitionPush时,有没有办法消除退出视图的淡出效果? - DNJohnson
@DJohnson,很有趣,我之前没有注意到这个问题。我尝试过了,但是无法正确修复它。只要你想从右侧推送,你可以覆盖第61-64行并在那里执行标准推送。 - lawicko
Xcode 7.2 生成了这个构建警告:“类别正在实现一个也将被其主要类实现的方法”,这是针对 standardAnimationWithController:duration:options:changesBlock 方法的。如何在不使用 pragma clang diagnostic 的情况下防止此警告? - lifjoy
@lifjoy 看起来这个特定方法存在名称冲突,只需将类别中的方法名称更改为其他名称,然后查看警告是否消失。该方法不应在类别源之外使用,因此此更改应该很容易。 - lawicko
谢谢lawicko,但重命名方法并没有消除警告。我通过在UINavigationController_Additions.m文件的顶部注释掉@interface UINavigationController()定义块来解决问题,因为它的方法不被外部调用者引用。 - lifjoy
显示剩余5条评论

2
您需要将QuartzCore.framework添加到您的目标中,以解决_OBJC_CLASS_$_CATransition错误。

0

最近我尝试创建自己的过渡效果,这是我制作的可重用库:

https://github.com/travisjeffery/TRVSNavigationControllerTransition

这是我的博客文章,讲述如何制作自己的转场效果。

基本思路很简单,只需对navigationController的(当前)视图进行CALayer快照,然后无动画地推出/弹回视图,对新视图进行CALayer快照,然后为这些层添加自己的动画,完成动画后再移除这些层。


拍摄快照会严重延迟过渡,尤其是在老设备上。 - Alex1987

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接