Thursday, 25 February 2016

Morph a view into another in Swift

Today I was tasked with morphing a button, when tapped, into another view that I'd already constructed of a bespoke date selector.

I achieved this by adding an extension to UIView, with a function that animates the bounds, frame and background colour of one view to another:


   func morphToView(duration:Double,delay:Double,toView:UIView){
        
        UIView.animateWithDuration(duration, delay: delay, usingSpringWithDamping: 1.0, initialSpringVelocity: 10, options: [], animations: {
            self.bounds = toView.bounds;
            self.frame = toView.frame
            self.backgroundColor = toView.backgroundColor
            
            }, completion: nil)

    }

making it super easy to use:

btnYes.morphToView(1.3, delay: 0.0, toView: dateSelector)

No comments:

Post a Comment

Comments are moderated, so you'll have to wait a little bit before they appear!