Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我试图取消模式时,iOS崩溃_Ios_Objective C_Core Animation_Modalviewcontroller - Fatal编程技术网

当我试图取消模式时,iOS崩溃

当我试图取消模式时,iOS崩溃,ios,objective-c,core-animation,modalviewcontroller,Ios,Objective C,Core Animation,Modalviewcontroller,我使用模态转换动画对控制器进行模态化。但是当我试图关闭控制器时,程序崩溃了。Xcode没有记录任何消息 我的过渡动画有问题。当我删除转换代码时,它运行良好 那么我的代码怎么了 - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext { _transitionContext = transitionContext; UIView *toView = [tr

我使用模态转换动画对控制器进行模态化。但是当我试图关闭控制器时,程序崩溃了。Xcode没有记录任何消息

我的过渡动画有问题。当我删除转换代码时,它运行良好

那么我的代码怎么了

- (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];
}
-(void)animateTransfion:(id)transitionContext{
_transitionContext=transitionContext;
UIView*toView=[transitionContext viewForKey:UITransitionContextToViewKey];
UIView*fromView=[transitionContext viewForKey:UITransitionContextFromViewKey];
CALayer*toVcLayer=[CALayer层];
toVcLayer.frame=toView.layer.frame;
[toVcLayer addSublayer:toView.layer];
_transformLayer=[CATTransformLayer层];
_transformLayer.frame=[UIScreen mainScreen].bounds;
[\u transformLayer addSublayer:fromView.layer];
CATTransformM3D ct=CATTransformM3D实体;
ct=CATTransformM3dMakeTransformation(0.0,0.0,-1.0);
ct=ct,M_-PI,0.0,1.0,0.0;
toVcLayer.transform=ct;
[_transformlayeraddsublayer:toVcLayer];
CATTransformM3D at=CATTransformM3D实体;
at=CATTransportorm3drotate(at,M_PI,0.0,1.0,0.0);
CABasicAnimation*动画=[CABasicAnimation animationWithKeyPath:@“transform”];
animation.toValue=[NSValue ValueWithCatTransformM3d:at];
animation.duration=1.0;
animation.delegate=self;
[_transformlayeraddanimation:animation forKey:nil];
[transitionContext.containerView.layer addSublayer:_transformLayer];
}
-(void)animationDidStop:(CAAnimation*)anim finished:(BOOL)标志{
[\u transitionContext.containerView添加子视图:[\u transitionContext viewForKey:UITransitionContextToViewKey]];
[_TransitionContextCompleteTransition:是];
}

问题可能在于在其他层之间移动层。您正在尝试在其他地方插入已具有超图层的图层。我不知道为什么它在演示时还没有崩溃,但它在演示时崩溃也就不足为奇了

解除视图控制器时,它们的状态不同。主要地,
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];
}
-(无效)动画转换(id)转换上下文{
_transitionContext=transitionContext;
UIView*toView=[transitionContext viewForKey:UITransitionContextToViewKey];
UIView*fromView=[transitionContext viewForKey:UITransitionContextFromViewKey];
if(toView.superview==nil)
[transitionContext.containerView添加子视图:toView];
const NSTimeInterval持续时间=1.0;
常数CGFloat translationX=400.0;
toView.layer.transform=CATTransferorM3DMAKETRANSFORM(translationX,0.0,0.0);
CABasicAnimation*toAnimation=[CABasicAnimation animationWithKeyPath:@“transform”];
toAnimation.toValue=[NSValue ValueWithCatTransformM3d:CatTransformM3DidEntity];
toAnimation.duration=持续时间;
toAnimation.delegate=self;
CABasicAnimation*fromAnimation=[CABasicAnimation animationWithKeyPath:@“transform”];
fromAnimation.toValue=[NSValue ValueWithCatTransformM3d:CatTransformM3DmakeTranslation(-translationX,0.0,0.0];
fromaniation.duration=持续时间;
[fromView addAnimation:fromAnimation-forKey:nil];
[toView addAnimation:toAnimation-forKey:nil];
}
-(void)animationDidStop:(CAAnimation*)anim finished:(BOOL)标志{
[_TransitionContextCompleteTransition:是];
}

问题可能在于在其他层之间移动层。您试图在其他地方插入已经有超层的层。我不知道为什么它在演示时还没有崩溃,但它在演示时会崩溃也就不足为奇了

解除视图控制器时,它们的状态不同。主要是,
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];
}
-(无效)动画转换(id)转换上下文{
_transitionContext=transitionContext;
UIView*toView=[transitionContext viewForKey:UITransitionContextToViewKey];
UIView*fromView=[transitionContext viewForKey:UITransitionContextFromViewKey];
if(toView.superview==nil)
[transitionContext.containerView添加子视图:toView];
const NSTimeInterval持续时间=1.0;
常数CGFloat translationX=400.0;
toView.layer.transform=CATTransferorM3DMAKETRANSFORM(translationX,0.0,0.0);
CABasicAnimation*toAnimation=[CABasicAnimation animationWithKeyPath:@“transform”];
toAnimation.toValue=[NSValue ValueWithCatTransformM3d:CatTransformM3DidEntity];
toAnimation.duration=持续时间;
toAnimation.delegate=self;
CABasicAnimation*fromAnimation=[CABasicAnimation animationWithKeyPath:@“transform”];
fromAnimation.toValue=[NSValue ValueWithCatTransformM3d:CatTransformM3DmakeTranslation(-translationX,0.0,0.0];
fromaniation.duration=持续时间;
[fromView addAnimation:fromAnimation-forKey:nil];
[toView addAnimation:toAnimation-forKey:nil];
}
-(void)animationDidStop:(CAAnimation*)anim finished:(BOOL)标志{
[_TransitionContextCompleteTransition:是];
}

谢谢!我对这个问题困惑了很长时间。谢谢!我对这个问题困惑了很长时间。