动画标签以增加和减小尺寸

时间:2021-03-14 07:11:25

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?

我可以做些什么改变才能让它按照我的意图去做?

2 个解决方案

#1


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)
}

#2


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.

在iOS 6和7中,在自动布局下应用于UIViews的变换UIView动画不起作用(因为自动布局会阻止它们)。但是,这在iOS 8中已得到修复。

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?

如果您坚持支持iOS 7,则有许多变通方法,包括使用CABasicAnimation(图层动画)而不是UIView动画。另请参阅我的论文:当使用自动布局时,如何调整CALayer的锚点?

#1


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)
}

#2


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.

在iOS 6和7中,在自动布局下应用于UIViews的变换UIView动画不起作用(因为自动布局会阻止它们)。但是,这在iOS 8中已得到修复。

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?

如果您坚持支持iOS 7,则有许多变通方法,包括使用CABasicAnimation(图层动画)而不是UIView动画。另请参阅我的论文:当使用自动布局时,如何调整CALayer的锚点?