Search code examples
swiftanimatewithduration

Animation of image through layoutIfNeeded stopped working


I have encountered an issue that's really confusing to me. I wrote a simple animation of two images to move and it worked perfectly. However all sudden it stopped animate and I can't figure out why. I haven't made any changes I can think of that would effect the animation. I have checked that the NSLayoutConstraints are actually being updated and they are, but it's not being animated.. Any ideas would be more than appreciated.

Unfortunately I don't have any git versions. I'm a bit of a newbie and at least this experience thought me of the value of git and I will use it henceforth. I have added constraints in the storyboard, four of them would be the ones I try to affect through the code. They are the y and x constraint for the two images I try to move.

Additional information I have four warnings saying: Constraint referencing items turned off in current configuration. Turn off this constraint in the current configuration. I have been able to conclude that the constraints giving the warnings are the four constraints I'm trying to animate. But I'm still unsure how to fix this.

Additional update I have now moved the animation code to override func viewWillAppear(animated: Bool) { super.viewWillAppear(true). After doing that I actually see the animation if I remove the first self.view.layoutIfNeeded() However it animates all the views. But I can't get only the constraint changes to animate still.

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

...

    self.view.layoutIfNeeded()
    self.firstCallyC?.constant = 0

    UIView.animateWithDuration(1.3, delay: 0.3, options: .CurveEaseInOut, animations: {
        self.firstCallyC!.constant = (usePoint[0].y) - 210
        self.firstCallxC?.constant = (usePoint[0].x) - 300
        self.meditateyC?.constant = (usePoint[179].y) - 210
        self.meditatexC?.constant = (usePoint[179].x) - 300
        self.view.layoutIfNeeded()
        }, completion: nil)
}

Solution

  • I finally found a solution by adding a override func viewDidAppear to the class and then adding the animation to that function. I guess all the images need to be properly loaded and placed before they can be animated or something like that?