gpt4 book ai didi

ios - 从第二个 View Controller 中关闭一组具有自定义动画的 Controller

转载 作者:行者123 更新时间:2023-11-30 13:22:37 25 4
gpt4 key购买 nike

显示第一个模态的主视图 Controller 操作

@IBAction func button(sender: AnyObject) {
let sb = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
if let vc = sb.instantiateViewControllerWithIdentifier("VC1") as? FirstViewController {
vc.modalPresentationStyle = .OverCurrentContext
vc.modalTransitionStyle = .CrossDissolve
presentViewController(vc, animated: true, completion: nil)
}
}

VC1 有一个自定义动画可以转换到第二个 View Controller

@IBAction func go(sender: AnyObject) {
guard let vc = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier("VC2") as? SecondViewController else {return}
vc.transitioningDelegate = transitionManager
vc.modalPresentationStyle = .CurrentContext
presentViewController(vc, animated: true, completion: nil)
}

VC2 - 只是一个驳回电话

@IBAction func dismiss(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}

VC2 不起作用。目前,它已将自己撤回 VC1。我希望它能够返回 Main 而无需先返回。

过渡经理

class TransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {

// TRANSITION PROTOCOL METHODS
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
guard let container = transitionContext.containerView() else {return}
guard let toView = transitionContext.viewForKey(UITransitionContextToViewKey) else {return}
guard let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey) else {return}

// set up from 2D transforms that we'll use in the animation
let width = container.frame.width
let offScreenRight = CGAffineTransformMakeTranslation(width, 0)
let offScreenLeft = CGAffineTransformMakeTranslation(-width, 0)

toView.transform = offScreenRight

container.addSubview(toView)
container.addSubview(fromView)

let duration = self.transitionDuration(transitionContext)



UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: .AllowAnimatedContent, animations: {
fromView.transform = offScreenLeft
toView.transform = CGAffineTransformIdentity
}) { finished in
transitionContext.completeTransition(true)
}
}

func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 1.4
}


// DELEGATE METHODS
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}

func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}

}

我尝试将其放入导航 Controller 中,通过 self.presentingViewController.dismissViewcontroller 以及其他一些东西来关闭。我觉得我错过了关于 View Controller 的一些非常基本的东西,这些东西将彻底打开。

Gif 说明它不应该如何工作:

now not

我希望 2 回家后不再见到 1

编辑:我已经做到了:

presentingViewController?.presentingViewController?.dismissViewControllerAnimated(false, 完成: nil)

但是感觉不对劲,看起来也不对劲。我采用这种风格的主要原因是因为实际项目中的模态具有半透明背景,因此普通的导航 Controller 或常规过渡会导致它们以丑陋的方式重叠。

这也是一种单向街道的事情。 VC1 不应该在 VC2 出现后出现,所以也许我只是认为这完全错误,不同风格的组件是最好的。

最佳答案

这样可以吗:

presentingViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)

编辑:演示,而不是父级

关于ios - 从第二个 View Controller 中关闭一组具有自定义动画的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37622346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com