Search code examples
iosswiftanimationxib

Swift: Load .Xib with animation


Currently I am loading a new .Xib of class CreateAnAccount: UIView form a button pressed on another view.

At the moment it immediately switches to the Xib which is great, but is there a way of animating this? below is the code in the button.

    @IBAction func createAnAccount(sender: AnyObject) {
    let createAnAccountView = NSBundle.mainBundle().loadNibNamed("CreateAnAccount", owner: self, options: nil)[0] as! CreateAnAccount
    createAnAccountView.frame = CGRectMake(0, 0, UIScreen.mainScreen().applicationFrame.size.width, UIScreen.mainScreen().applicationFrame.size.height + 20)
    createAnAccountView.loginHandler = loginHandler
    self.addSubview(createAnAccountView)
    println("Create An Account Pressed")
}

Solution

  • Swift 4. Just place your animation code into layoutSubviews func. Works perfect for me.

    override func layoutSubviews() {
        super.layoutSubviews()
        animate()
    }
    
    func animate() {
        self.transform = CGAffineTransform(scaleX: 0.3, y: 2)
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0, options: [.allowUserInteraction, .curveEaseOut], animations: {
            self.transform = .identity
        })
        self.alpha = 1
    }