我如何得到这个动画渐变工作? [英] How do I get this fade animation to work?
问题描述
我想建立一个简单的脉动环,将突出一个按钮,以提示用户推荐的选择。我做了简单的淡入()和淡出()函数,设置在淡出()的延迟,因此它会触发为淡入()完成......或者至少这是我的本意。什么样的结果是,当按钮pressed,环瞬间变为阿尔法= 1,然后2秒的延迟,然后褪色超过2秒。有一次,我有一个周期的工作,我希望把它变成一个循环继续下去,而需要的。
I'm trying to set up a simple pulsating ring that will highlight a button, to prompt the user to the recommended choice. I made simple fadeIn() and fadeOut() functions, set a delay in the fadeOut(), so that it will trigger as the fadeIn() completes...or at least that's what I intended. What results is, when the button is pressed, the ring instantly goes to alpha = 1, then delays for 2 seconds, and then fades over 2 seconds. Once I have a cycle working, I'd like to put it into a loop to keep it going while needed.
如预期,如果单独执行的功能,要么将工作。我发现另一个线程,与简单的淡入淡出或交易,但不能同时在一起。
Either function will work as intended, if executed alone. I found another thread that deals with simple fadeIn or fadeOut, but not both together.
我敢肯定,code是做什么我告诉它做什么,但我不明白,为什么它不工作。有人能指出我的错误呢?谢谢。
I'm sure the code is doing exactly what I'm telling it do, but I can't figure out why it's not working. Can someone point to my mistake? Thanks.
@IBOutlet weak var greenRing: UIImageView!
@IBAction func buttonPressed(sender: AnyObject)
{
fadeIn()
fadeOut()
}
func fadeIn() {
UIImageView.animateWithDuration(2.0, animations: {
self.greenRing.alpha = 1
})
}
func fadeOut() {
UIImageView.animateWithDuration(2.0, delay: 2.0, options: nil, animations: {
self.greenRing.alpha = 0
}, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
greenRing.alpha = 0
}
}
推荐答案
可以实现与一个重复的动画你想要的效果。通 .Autoreverse | .Repeat
为选项:
到 animateWithDuration
,它会自动反转并重复淡入
You can achieve your desired effect with one repeating animation. Pass .Autoreverse | .Repeat
as the options:
to animateWithDuration
and it will automatically reverse and repeat the fadeIn.
当按钮被pressed,您可以通过调用 greenRing.layer.removeAllAnimations()
When the button is pressed, you can stop the animation by calling greenRing.layer.removeAllAnimations()
动画也可能会看起来更好,如果你还添加选项 UIViewAnimationOption.CurveEaseInOut
。所以:
The animation will also likely look nicer if you also add the option UIViewAnimationOption.CurveEaseInOut
. So:
.Autoreverse | .Repeat | .CurveEaseInOut
这篇关于我如何得到这个动画渐变工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!