1

I have a very simple project, I want to animate the right margin on a label, however when I try it, it finishes instantly.

@IBAction func onDo(sender:UIButton)
{
    self.view.setNeedsLayout()
    self.testConstraint.constant = 40.0

    UIView.animateWithDuration(2.0, animations: { () -> Void in

        self.view.setNeedsLayout()

        }) { (complete:Bool) -> Void in

    }
}

The project is here:

https://www.dropbox.com/s/9a0v0906riunkww/test2222.zip?dl=0

Am I missing something obvious?

Update #1

It seems it's a problem with UILabels specifically, a standard UIView, or UIButton animates fine....so whats wrong with a UILabel?

Update #2

If I set the UILabel View Content Mode to Center, then it works fine, but it doesn't look very smooth...very strange.

Chris
  • 2,739
  • 4
  • 29
  • 57

2 Answers2

0

I may be off the mark here, but it looks to me as though your animation routine just does setNeedslayout, which will cause the screen to redraw instantly. You need to change some animatable parameter of the UIView in order to see something move. Like self.view.frame, perhaps. The animatable properties of UIView are listed here.

emrys57
  • 6,679
  • 3
  • 39
  • 49
0

Try replacing self.view.setNeedsLayout() in animation closure with self.view.layoutIfNeeded()

@IBAction func onDo(sender:UIButton) {
    self.testConstraint.constant = 40.0

    UIView.animateWithDuration(2.0, animations: { () -> Void in

        self.view.layoutIfNeeded()

        }) { (complete:Bool) -> Void in

    }
}
Afnan
  • 888
  • 1
  • 13
  • 37