Maxim Poltoratsky Maxim Poltoratsky - 5 months ago 7
iOS Question

UIView.animateWithDuration going out of the bounds

I'm trying to animate some text field. That start animation outside the screen and moving to the center of the screen. But in my case, the animation starts from the center and going out of the bounds. That happens when I declaring this in viewWillAppear/viewDidAppear, but in viewDidLoad all work's fine/

override func viewWillAppear(animated: Bool) {
emailField.center.x -= view.bounds.width
}

override func viewDidAppear(animated: Bool) {


UIView.animateWithDuration(0.5, delay: 0.4, options: [], animations: {
self.emailField.center.x += self.view.bounds.width
}, completion: nil)


}

Answer

You could try animating the transform property.

override func viewWillAppear(animated: Bool) {
    emailField.transform = CGAffineTransformMakeTranslation(-view.bounds.width, 0.0)
}

override func viewDidAppear(animated: Bool) {
    UIView.animateWithDuration(0.5, delay: 0.4, options: [], animations: {
        self.emailField.transform = CGAffineTransformIdentity
    }, completion: nil)
}