Search code examples
iosuiviewanimatewithduration

UIView.animatewithDuration not animating when activated by button


So I have a bunch of arrows that I transform once in the viewDidLoad() function like this:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    UIView.animateWithDuration(2.0) {

        self.Arrow1.transform = CGAffineTransformMakeTranslation(-20, 0)
        self.Arrow1.transform = CGAffineTransformRotate(self.Arrow1.transform, -66.0 * CGFloat(M_PI)/180.0)


        self.Arrow2.transform = CGAffineTransformMakeTranslation(-9, -8)
        self.Arrow2.transform = CGAffineTransformRotate(self.Arrow2.transform, -70.0 * CGFloat(M_PI)/180.0)

        // And So On...

    }

}

but then I try to later move them at the press of a button:

   @IBAction func searchPressed(sender: AnyObject) {
        UIView.animateWithDuration(5.0) {
            self.Arrow1.transform = CGAffineTransformTranslate(self.Arrow1.transform, -100, 0)

            self.Arrow2.transform = CGAffineTransformTranslate(self.Arrow2.transform, 0, 100)

           //And So on... (They're 9 Arrows)

        }
    }

But then it doesn't animate, or I think it does, but really weirdly. It moves a lot instantly, then starts animating from there. I don't understand why or how I can fix it. Here is what happens


Solution

  • For future travelers stuck at the same predicament: You have to control your views using autolayout if they're subview of views using autolayout as well. Otherwise you get the weird results I was getting.

    Here is an excellent guide on how to do that.