我有两个视图控制器,第一个称为“登录视图控制器”,后跟导航控制器,然后是另一个名为“注册视图控制器”的视图控制器。我在网上发现了一篇文章,详细介绍了执行展开segue动画的正确方法,并将此代码放在“Login View Controller”中
override func segueForUnwinding(to toViewController: UIViewController,
from fromViewController: UIViewController,
identifier: String?) -> UIStoryboardSegue {
return UIStoryboardSegue(identifier: identifier, source: fromViewController, destination: toViewController) {
let fromView = fromViewController.view
let toView = toViewController.view
if let containerView = fromView?.superview {
let initialFrame = fromView?.frame
var offscreenRect = initialFrame
offscreenRect?.origin.x -= (initialFrame?.width)!
toView?.frame = offscreenRect!
containerView.addSubview(toView!)
// Being explicit with the types NSTimeInterval and CGFloat are important
// otherwise the swift compiler will complain
let duration: TimeInterval = 1.0
let delay: TimeInterval = 0.0
let options = UIViewAnimationOptions.curveEaseInOut
let damping: CGFloat = 0.5
let velocity: CGFloat = 4.0
UIView.animate(withDuration: duration, delay: delay, usingSpringWithDamping: damping,
initialSpringVelocity: velocity, options: options, animations: {
toView?.frame = initialFrame!
}, completion: { finished in
toView?.removeFromSuperview()
if let navController = toViewController.navigationController {
navController.popToViewController(toViewController, animated: false)
}
})
}
}
当我按下“注册视图控制器”内部的后退按钮时,现在发生的是代码成功执行但从视图中删除“登录视图控制器”并停留在“注册视图控制器”上。将“toView?.removeFromSuperview()”切换为“fromView?.removeFromSuperview()”后,它成功停留在“登录视图控制器”上,但现在如果按下按钮将我带到“注册视图控制器”,它就会发送我回到“登录视图控制器”本质上创建一个无限循环。