更改animation转换

我有一个与NavigationController的应用程序。 我如何更改pushViewControllerpopToViewControlleranimation过渡样式?

UPD

我创build类似@lawicko的答案。 但是当我试图调用函数的时候出错了

[self.navigationController pushViewController:放置withCustomTransition:CustomViewAnimationTransitionPush子types:CustomViewAnimationSubtypeFromLeft];

错误是:“使用未声明的标识符'CustomViewAnimationTransitionPush'”

我应该在哪里申报这部分:

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

现在写我宣布它在UINavigationController+Additions.h

UPD 2:另外一个新的错误:

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

和所有的_kCATransitions相同的错误

看看我创build的这个UINavigationController类别。 它允许你在几乎所有可能的转换中推送和popup,还支持QuartzCore转换的子types,这将允许你做到你想要的 – 从左侧推视图。 像这样做:

 [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 

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

我最近解决了创build自己的转换,这是我创build的可重用的库:

https://github.com/travisjeffery/TRVSNavigationControllerTransition

这里是我的博客文章,谈论如何使自己的过渡。

基本的想法非常简单,只需要导航控制器的(当前)视图的CALayer快照,然后推动/popup视图没有animation,采取CALayer快照的新视图,然后添加自己的animation到这些层,然后一旦animation完成,移除这些图层。