Remove animation in swift

强颜欢笑 提交于 2019-12-09 05:23:00

问题


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.


回答1:


//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({$0.layer.removeAllAnimations()})
    self.view.layer.removeAllAnimations()
    self.view.layoutIfNeeded()
}


来源:https://stackoverflow.com/questions/39443163/remove-animation-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!