简体   繁体   English

在segue中如何访问子视图

[英]How do I access subviews during a segue

I'm new to iOS programming and I'm trying to make a simple segue in which subviews of both the source and destination views are animated. 我是iOS编程的新手,我想做一个简单的选择,在其中将源视图和目标视图的子视图动画化。

I'm having trouble accessing subviews on my source view in my segue class in order to animate them. 我无法在segue类中访问源视图中的子视图以设置它们的动画。 What is the easiest way to access and animate subviews of the source view during a segue? 在segue期间访问源视图的子视图并为其设置动画的最简单方法是什么?

Here is a simplified version of my code. 这是我的代码的简化版本。

// 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
        })
    }
}

You can use this code: 您可以使用以下代码:

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

Another option: If you want to animate something as you transition away from a scene, you can animate alongside the transition: 另一种选择:如果要在离开场景过渡时为某物设置动画,则可以在过渡旁边设置动画:

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)
}

Also, another alternative to subclassing segue, you can do custom transitions. 此外,作为继承segue的另一种选择,您可以执行自定义转换。 See WWDC 2013 Custom Transitions Using View Controllers . 请参阅WWDC 2013 使用视图控制器的自定义过渡 It depends upon the nature of the transition and associated animations. 这取决于过渡和相关动画的性质。 But this is a great way to do really robust and rich transitions. 但这是进行强大而丰富的过渡的好方法。

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

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