Nathan Manousos
Nathan Manousos

Reputation: 13848

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

Upvotes: 0

Views: 155

Answers (2)

Rob
Rob

Reputation: 437381

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.

Upvotes: 0

marosoaie
marosoaie

Reputation: 2361

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

Upvotes: 1

Related Questions