gpt4 book ai didi

ios - 在关闭模态视图 Controller 的动画时访问 presentingViewController

转载 作者:行者123 更新时间:2023-11-28 08:18:44 26 4
gpt4 key购买 nike

我在主视图 Controller 中有一个按钮,它以模态方式呈现另一个 View Controller 。当模态 vc 向上滑动时,我制作了一个主视图 Controller 变暗的自定义动画。但是,当模态 vc 向下滑动(使用 dismiss)时,我怎样才能让它倒退呢?使用 this site 上的教程,我创建了访问呈现 vc 的协议(protocol),但动画并没有发生,它只是立即改变了不透明度。

此外,如果我将动画代码放入呈现 vc 的 viewWillAppear 中,它只会在模态 vc 一直向下滑动(消失)时启动动画。我没有使用常规的 show segue,因为我认为这不是摆脱模态 vc 的最佳方式。如果我错了,请指正

这是presenting vc中的prepareForSegue

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let identifier = segue.identifier{
switch identifier {
case "showSettings":
let tempFrame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
dimView = UIView(frame: tempFrame)
dimView.backgroundColor = .black
dimView.layer.opacity = 0
self.view.addSubview(dimView)
UIView.animate(withDuration: 0.5, animations: {
self.dimView.layer.opacity = 0.4
})
default:break
}
}
if let vc = segue.destination as? Dismissable
{
vc.dismissalDelegate = self
}
}

这是一个 IBaction,它在自身内部消除模态 vc。

    @IBAction func dismiss() {
dismissalDelegate?.finishedShowing(viewController: self)
UIView.animate(withDuration: 1.0, animations:{
(self.dismissalDelegate as! ViewController).dimView.layer.opacity = 0.3
})
}

最佳答案

这只是一个如何实现的例子。很确定它远非完美,但应该作为一个起点。那里有很多特定于我的旧项目的代码,但可以删除。

final class ModalSeguePushStyle : UIStoryboardSegue
{
override func perform() {
let sourceVC = self.sourceViewController
let destinationVC = self.destinationViewController
var endPosition = destinationVC.view.frame
var startPostition = destinationVC.view.frame

startPostition.origin.x = startPostition.width

if sourceVC.navigationController != nil
{
startPostition.origin.y = -44
endPosition.origin.y = -44
}

destinationVC.view.frame = startPostition

sourceVC.view.addSubview(destinationVC.view)

UIView.animateWithDuration(0.3, animations: { () -> Void in
destinationVC.view.frame = endPosition
}) { (Bool) -> Void in
self.sourceViewController.presentViewController(self.destinationViewController, animated: false, completion: nil)
}
}
}

final class ModalSeguePushStyleUnwind : UIStoryboardSegue
{
func createMockView(view: UIView) -> UIImageView {
UIGraphicsBeginImageContextWithOptions(view.frame.size, true, UIScreen.mainScreen().scale)

view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()
return UIImageView(image: image)
}

override func perform() {
let sourceVC = self.sourceViewController
let destinationVC = self.destinationViewController
var endPosition = sourceVC.view.frame

endPosition.origin.x += endPosition.width

var bar:UIImageView?
let mock = createMockView(destinationVC.view)

if destinationVC.navigationController != nil
{
mock.frame.origin.y = 64
}

sourceVC.view.superview!.insertSubview(mock, atIndex: 0)

if let b = destinationVC.tabBarController?.tabBar
{
bar = createMockView(b)
bar?.frame = b.frame
sourceVC.view.superview!.insertSubview(bar!, aboveSubview: mock)
}

UIView.animateWithDuration(0.3, animations: { () -> Void in
sourceVC.view.frame = endPosition
}) { (Bool) -> Void in
sourceVC.dismissViewControllerAnimated(false, completion: { () -> Void in

})
}
}
}

关于ios - 在关闭模态视图 Controller 的动画时访问 presentingViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41861074/

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