我正在尝试向我现有的iOS应用添加自定义转换。它似乎一直在崩溃。我的应用程序有一个主导航控制器,我已经分类。导航控制器的根控制器充当UINavigationController的委托。
PS:我也尝试使自定义导航控制器成为自己的委托,但在调用animationControllerForOperation委托方法之前它会崩溃。
这是我的代码
自定义导航控制器
@interface MainNavigationViewController : UINavigationController
@end
@implementation MainNavigationViewController
- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC
{
if ([fromVC isKindOfClass:[FlowLayoutCategoryController class]] && [toVC isKindOfClass:[FlowLayoutCategoryController class]]) {
if (operation == UINavigationControllerOperationPush) {
CategoryTransitionAnimation *animation = [CategoryTransitionAnimation new];
animation.type = TransitionTypePush;
return animation;
}
else if (operation == UINavigationControllerOperationPop) {
CategoryTransitionAnimation *animation = [CategoryTransitionAnimation new];
animation.type = TransitionTypePop;
return animation;
}
else {
return nil;
}
}
return nil;
}
过渡动画类
typedef enum
{
TransitionTypePush,
TransitionTypePop
} TransitionType;
@interface CategoryTransitionAnimation : NSObject<UIViewControllerAnimatedTransitioning>
@property (nonatomic, assign) TransitionType type;
@end
#import "CategoryTransitionAnimation.h"
@implementation CategoryTransitionAnimation
#pragma mark - UIViewControllerAnimatedTransitioning
- (void) animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
//Get references to the view hierarchy
UIView *containerView = [transitionContext containerView];
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
if (self.type == TransitionTypePush) {
//Add 'to' view to the hierarchy with 0.0 scale
toViewController.view.transform = CGAffineTransformMakeScale(0.0, 0.0);
[containerView insertSubview:toViewController.view aboveSubview:fromViewController.view];
//Scale the 'to' view to to its final position
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
toViewController.view.transform = CGAffineTransformMakeScale(1.0, 1.0);
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
} else if (self.type == TransitionTypePop) {
//Add 'to' view to the hierarchy
[containerView insertSubview:toViewController.view belowSubview:fromViewController.view];
//Scale the 'from' view down until it disappears
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
fromViewController.view.transform = CGAffineTransformMakeScale(0.0, 0.0);
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
}
- (NSTimeInterval) transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 1.0;
}
@end
答案 0 :(得分:0)
我要猜测问题是你省略了一个步骤:你应该为“to”视图控制器获取finalFrameForViewController:
并将视图的帧设置为该值。只是一个猜测,但我不禁注意到你没有给出任何框架的视图,这可能不是很好。