Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating a label to increase and decrease in size

Tags:

swift

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?

like image 336
user3746428 Avatar asked Aug 03 '14 01:08

user3746428


2 Answers

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)
}
like image 186
Louis Zhu Avatar answered Oct 12 '22 17:10

Louis Zhu


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?

like image 35
matt Avatar answered Oct 12 '22 17:10

matt