Remove animation in swift

2020-05-26 07:35发布

问题:

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