自定义segue类因线程1而崩溃:EXC_BAD_ACCESS(代码= 2,地址= 0x16ecb3ff0)

时间:2019-04-15 00:56:29

标签: swift uistoryboardsegue

我正在尝试创建一个具有通用背景图片的应用。由于我的大多数ViewController背景都设置为透明色,因此对于大多数过渡来说,这都是一个问题。我的解决方案是尝试创建此自定义segue类,该类将源VC推到左侧,并将新VC推到右侧。但是我收到此错误:

  

线程1:EXC_BAD_ACCESS(代码= 2,地址= 0x16ecb3ff0

在界面构建器中,情节提要Segue:我链接了自定义类,并将种类设置为“自定义”。动画已选中。此设置可以从我的登录屏幕VC到“忘记密码VC”使用,但是如果我在联系人VC上使用它来添加联系人VC则无法使用。

不确定为什么要在一个而不是另一个上工作吗?我只是做错了吗? (我是iOS开发和学习的新手)

class PushSegue: UIStoryboardSegue {

    override func perform() {
        push()
    }

    func push () {
        let toViewController = self.destination
        let fromViewController = self.source

        let containerView = fromViewController.view.superview
        let originalCenter = fromViewController.view.center

        fromViewController.view.transform = CGAffineTransform(translationX: 0, y: 0)
        fromViewController.view.center = originalCenter

        toViewController.view.transform = CGAffineTransform(translationX: 600, y: 0)
        toViewController.view.center = originalCenter

        containerView?.addSubview(toViewController.view)

        UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseInOut, animations: { toViewController.view.transform = CGAffineTransform.identity
            fromViewController.view.transform = CGAffineTransform (translationX: -600, y: 0)
        }, completion: {success in
            fromViewController.present(toViewController, animated: false, completion: nil) //Crashes here
        })

    }

}

class UnwindPushSegue: UIStoryboardSegue {

    override func perform() {
        push()
    }

    func push () {
        let toViewController = self.destination
        let fromViewController = self.source

        fromViewController.view.superview?.insertSubview(toViewController.view, at: 0)
        toViewController.view.superview?.insertSubview(toViewController.view, at: -600)

        UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseInOut, animations: { fromViewController.view.transform = CGAffineTransform.init(translationX: 600, y: 0)
            toViewController.view.transform = CGAffineTransform.init(translationX: 0, y: 0)
        }, completion: {success in
            fromViewController.dismiss(animated: false, completion: nil)
        })

    }
}

我希望源视图控制器向左移动,但是不会向左移动。新的VC是从右侧插入的,但是我仍然可以在下面看到源视图控制器。

0 个答案:

没有答案