繁体   English   中英

在segue中如何访问子视图

[英]How do I access subviews during a segue

我是iOS编程的新手,我想做一个简单的选择,在其中将源视图和目标视图的子视图动画化。

我无法在segue类中访问源视图中的子视图以设置它们的动画。 在segue期间访问源视图的子视图并为其设置动画的最简单方法是什么?

这是我的代码的简化版本。

// Source view controller
class ViewController: UIViewController {
    @IBOutlet weak var myButton: UIButton!
    // other normal view controller code
}

class mySegue: UIStoryboardSegue {
    override func perform() {
        UIView.animate(withDuration: 1.0, animations: {
            // how do I access and animate myButton?
            // source.myButton
        })
    }
}

您可以使用以下代码:

class MySegue: UIStoryboardSegue {
    override func perform() {
        UIView.animate(withDuration: 1.0, animations: {
            let sourceController = self.source as! ViewController
            sourceController.myButton
        })
    }
}

另一种选择:如果要在离开场景过渡时为某物设置动画,则可以在过渡旁边设置动画:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    transitionCoordinator?.animate(alongsideTransition: { context in
        self.myButton.backgroundColor = .red            // animate change of background color during transition
    }, completion: nil)
}

此外,作为继承segue的另一种选择,您可以执行自定义转换。 请参阅WWDC 2013 使用视图控制器的自定义过渡 这取决于过渡和相关动画的性质。 但这是进行强大而丰富的过渡的好方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM