1

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")
}
Dom Bryan
  • 1,238
  • 2
  • 19
  • 39
  • 1
    This is in Objective C but it's a good start for you: http://stackoverflow.com/questions/2337408/addsubview-animation – Fred Faust Aug 06 '15 at 15:31
  • To animate view there is lot in iOS, First need to understand what type of animation you need for your nib. – Paresh. P Jan 11 '19 at 08:15

3 Answers3

1

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
}
coldembrace
  • 549
  • 8
  • 19
1

Swift 5. @coldembrace answer

func animate() {
        self.view.transform = CGAffineTransform(scaleX: 0.3, y: 2)
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0, options: [.allowUserInteraction, .curveEaseOut], animations: {
            self.view.transform = .identity
        })
        self.view.alpha = 1
    }
SwiftyLifestyle
  • 416
  • 5
  • 17
0

You can try like this

UIView.transitionWithView(self.view, duration: 0.5, options:UIViewAnimationOptions.CurveEaseInOut,animations: {self.view.addSubview(createAnAccountView)}, completion: nil)  

Moreover you change the UIViewAnimationOptions to have a desired effect.

kushanky
  • 167
  • 1
  • 9
  • This still doesn't seem to do the trick, the view indeed changes but there is no animation. are you sure this can be used on a .Xib? – Dom Bryan Aug 06 '15 at 18:00