5

I have been trying to implement an animation that brings the users attention to a change in value in a label. I want to do this by quickly increasing and reducing the size of the label (can't think of a better way to describe it) and I've made some progress towards this. The problem is that while the animation increases in size as I want it to; the way it deceases in size isn't smooth. Also, once the animation is complete, the size of the font does not return to the original.

Here is what I have:

func bloat() {
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDelegate(self)
    UIView.setAnimationDelay(0.6)
    UIView.setAnimationDuration(0.3)
    UIView.setAnimationRepeatCount(4)
    UIView.setAnimationCurve(UIViewAnimationCurve.EaseInOut)
    currentBudgetDisplay.transform = CGAffineTransformMakeScale(0.9, 0.9)
    UIView.commitAnimations()
}

What changes can I make to get it to work as I intend it to?

Humayun Shabbir
  • 2,961
  • 4
  • 20
  • 33
user3746428
  • 11,047
  • 20
  • 81
  • 137
  • Just FYI: You should not be using UIView `beginAnimations` / `commitAnimations` unless you have to (though I know why you are doing it - it's because otherwise setting the repeat count is nontrivial). You should use `UIView.animationWithDuration...`. – matt Aug 03 '14 at 03:31

2 Answers2

7

The requirement is simple using core animation, try this

func bloat() {
    var animation = CABasicAnimation(keyPath: "transform.scale")
    animation.toValue = NSNumber(float: 0.9)
    animation.duration = 0.3
    animation.repeatCount = 4.0
    animation.autoreverses = true
    currentBudgetDisplay.layer.addAnimation(animation, forKey: nil)
}
Louis Zhu
  • 792
  • 5
  • 12
1

In iOS 6 and 7, transform UIView animations applied to UIViews under auto layout do not work (because the auto layout stops them). This is fixed in iOS 8, however.

If you insist on supporting iOS 7, there are many workarounds, including using CABasicAnimation (layer animation) instead of UIView animation. See also my essay here: How do I adjust the anchor point of a CALayer, when Auto Layout is being used?

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141