2017-06-22 9 views
1

モーダルのトランジションアニメーションを持つコントローラをモーダルにします。しかし、私がコントローラを却下しようとすると、プログラムがクラッシュします。 Xcodeは何のメッセージも記録しませんでした。モーダルを解除しようとするとiOSがクラッシュする

私のトランジションアニメーションに問題があります。トランジションコードを削除すると、トランジションコードは正常に動作します。

私のコードで何が問題になっていますか?

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext { 
    _transitionContext = transitionContext; 
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey]; 
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey]; 
    CALayer *toVcLayer = [CALayer layer]; 
    toVcLayer.frame = toView.layer.frame; 
    [toVcLayer addSublayer:toView.layer]; 

    _transformLayer = [CATransformLayer layer]; 
    _transformLayer.frame = [UIScreen mainScreen].bounds; 
    [_transformLayer addSublayer:fromView.layer]; 
    CATransform3D ct = CATransform3DIdentity; 
    ct = CATransform3DMakeTranslation(0.0, 0.0, -1.0); 
    ct = CATransform3DRotate(ct, M_PI, 0.0, 1.0, 0.0); 
    toVcLayer.transform = ct; 
    [_transformLayer addSublayer:toVcLayer]; 

    CATransform3D at = CATransform3DIdentity; 
    at = CATransform3DRotate(at, M_PI, 0.0, 1.0, 0.0); 
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
    animation.toValue = [NSValue valueWithCATransform3D:at]; 
    animation.duration = 1.0; 
    animation.delegate = self; 
    [_transformLayer addAnimation:animation forKey:nil]; 

    [transitionContext.containerView.layer addSublayer:_transformLayer]; 
} 

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { 
    [_transitionContext.containerView addSubview:[_transitionContext viewForKey:UITransitionContextToViewKey]]; 
    [_transitionContext completeTransition:YES]; 
} 

答えて

0

問題はおそらく他のレイヤー間でレイヤーを移動することにあります。既に他の場所にスーパーレイヤーがあるレイヤーを挿入しようとしています。私はなぜそれが提示するときに既にクラッシュしないのか分からないが、それは却下しても驚きではない。

ビューコントローラを閉じると、それらの状態は異なります。主にtoViewはすでにサブビューがcontainerViewです。結局のところ提示するとfromViewでした。あなたのコードビュー/レイヤー階層はかなり絡まってしまい、最終的にはクラッシュにつながります。

解決方法はかなり簡単です。別のレイヤーを保持し、アニメーション化するために別々のtransformLayerを作成する代わりに。それぞれのレイヤーを個別にアニメートします。例:

-(void)animateTransition(id<UIViewControllerContextTransitioning>)transitionContext { 
    _transitionContext = transitionContext; 
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey]; 
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey]; 

    if (toView.superview == nil) 
     [transitionContext.containerView addSubview:toView]; 

    const NSTimeInterval duration = 1.0; 
    const CGFloat translationX = 400.0; 

    toView.layer.transform = CATransform3DMakeTranslation(translationX, 0.0, 0.0); 

    CABasicAnimation *toAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
    toAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; 
    toAnimation.duration = duration; 
    toAnimation.delegate = self; 

    CABasicAnimation *fromAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
    fromAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(-translationX, 0.0, 0.0]; 
    fromAnimation.duration = duration; 

    [fromView addAnimation:fromAnimation forKey:nil]; 
    [toView addAnimation:toAnimation forKey:nil]; 
} 

-(void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag { 
    [_transitionContext completeTransition:YES]; 
} 
+0

ありがとう!私はこの問題を長い間混乱させてきました。 – hecong

関連する問題