21

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.

Almazini
  • 1,825
  • 4
  • 25
  • 48

1 Answers1

55
//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()
}
Adrian
  • 16,233
  • 18
  • 112
  • 180
Rutvik Kanbargi
  • 3,398
  • 2
  • 16
  • 7