ios 快速删除动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39443163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove animation in swift
提问by Almazini
I have a text field where user should enter info. And a label which points user to text field (like a hint).
我有一个文本字段,用户应该在其中输入信息。以及将用户指向文本字段的标签(如提示)。
I want to stop animation and remove hint label once user presses the text field to enter data.
一旦用户按下文本字段输入数据,我想停止动画并删除提示标签。
There is repeating animation on text label. Was created by:
文本标签上有重复动画。创建者:
override func viewDidLoad() {
super.viewDidLoad()
textInput.addTarget(self, action: #selector(CalculatorViewController.removeAnimation(_:)), forControlEvents: UIControlEvents.TouchDown)
self.hintLabel.alpha = 0.0
UIView.animateWithDuration(1.5, delay: 0, options: .Repeat
, animations: ({
self.hintLabel.alpha = 1.0
}), completion: nil
)
After it I have created a function to remove annotation
之后我创建了一个删除注释的函数
func removeAnimation(textField: UITextField) {
view.layer.removeAllAnimations()
self.view.layer.removeAllAnimations()
print("is it working?!")
}
Should work according to documentation.
应该根据文档工作。
My label keeps flashing even though I see the string printed in console. I guess problem is that animation is repeated but have no clue how to resolve this issue.
即使我看到控制台中打印的字符串,我的标签也会一直闪烁。我想问题是动画重复了,但不知道如何解决这个问题。
回答by Rutvik Kanbargi
//Just remove the animation from the label. It will Work
func remove()
{
self.hintLabel.layer.removeAllAnimations()
self.view.layer.removeAllAnimations()
self.view.layoutIfNeeded()
}
Update:
更新:
If you want to go nuclear, you could do this, as well:
如果你想去核,你也可以这样做:
func nukeAllAnimations() {
self.view.subviews.forEach({##代码##.layer.removeAllAnimations()})
self.view.layer.removeAllAnimations()
self.view.layoutIfNeeded()
}