Group Group Group Group Group Group Group Group Group

Animation doesn't work when I apply autolayout before the running?

#1

I just start to read this ebook, and after reading to chapter 2, I want to implement it to my own project, but ‘something weird’ happens.

so let say I have a button. and I implement autolayout before the running. I make the button horizontally in container. and then I want to animate this button like in the chapter 1.

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

    theButton.center.x -= view.bounds.width
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    UIView.animate(withDuration: 1, delay: 0, options: [], animations: {
        self.theButton.center.x += self.view.bounds.width
    }, completion: nil)
}

but it seems that the button start moving from the center to the right side…

if I remove all constraints of the button, it will work as I expect. but how to make animation and also I have autolayout contraint in it?

is it explained in in the book? am I jumping the gun since I just read to chapter 2? Thanks :slight_smile:

#2

@agung Thanks very much for your question!

Your constraints are holding the button in place which is probably why your button is unable to animate. The constraints are overriding the animation code.

My advice would be to use AutoLayout for the animation by using the constraints in the animation code itself. This means changing the constraints within the animation code as opposed to simply changing the coordinates. This is discussed in the book, so I would suggest you perhaps skip to that chapter to see how to do it :slight_smile:

I hope this helps!

All the best!

closed #3

This topic was automatically closed after 166 days. New replies are no longer allowed.