【问题标题】:iOS 7 App crashes with custom transitionsiOS 7 应用程序因自定义过渡而崩溃
【发布时间】:2014-02-03 03:59:48
【问题描述】:

我正在尝试向我现有的 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

【问题讨论】:

  • 如果它崩溃了,那么你的代码是错误的。但是您没有显示任何代码,因此没有人知道问题所在。您想查看一些有效的自定义转换代码吗?我在我的 github 站点中有很多:github.com/mattneub/Programming-iOS-Book-Examples 看看从 bk2ch06p292customAnimation1 开始的 6 个示例
  • 我已经添加了我的代码。我尝试在我的代码中添加断点,但在进入我的任何代码之前应用程序崩溃了。
  • 它在您的代码中 somewhere 崩溃;没有代码的项目不会崩溃。创建一个全局异常断点,以便您可以在异常被抛出的地方捕获它。
  • 顺便说一句,它似乎在您的 animateTransition: 方法中崩溃了。因此,您可以在它的开头设置一个断点,然后遍历以找到它崩溃的地方。你的代码看起来很简单,所以我相信我们能找到位置!

标签: ios7 uinavigationcontroller uiviewanimationtransition


【解决方案1】:

我将猜测问题在于您省略了一个步骤:您应该为“to”视图控制器获取finalFrameForViewController: 并将视图的框架设置为该值。只是一个猜测,但我不禁注意到你没有给视图 any 框架,这不是很好。

【讨论】:

    猜你喜欢
    • 2013-07-16
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    相关资源
    最近更新 更多