简体   繁体   中英

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.

I'm having trouble accessing subviews on my source view in my segue class in order to animate them. What is the easiest way to access and animate subviews of the source view during a 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. See WWDC 2013 Custom Transitions Using View Controllers . It depends upon the nature of the transition and associated animations. But this is a great way to do really robust and rich transitions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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