Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove animation in swift

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.

enter image description here

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.

like image 313
Almazini Avatar asked Sep 12 '16 03:09

Almazini


1 Answers

//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()
}
like image 80
Rutvik Kanbargi Avatar answered Sep 23 '22 21:09

Rutvik Kanbargi