IOS animation does not reset when replaying

I have a very simple animation block that performs the following animation:

enter image description here
(tap the image if the animation does not play)

func didTapButton() {
  // reset the animation to 0
  centerYConstraint.constant = 0
  superview!.layoutIfNeeded()

  UIView.animate(
    withDuration: 1,
    animations: {
      // animate the view downwards 30 points
      self.centerYConstraint.constant = 30
      self.superview!.layoutIfNeeded()
  })
}

Everything is fine when I play the animation myself. It resets to position 0, and then revives 30 points.

The problem is that the user briefly presses the button several times in a row (i.e., in the middle of the current animation). Each time the user clicks the button, I expect it to be reset to position 0, and then go down 30 points. Instead, I get the following:

enter image description here
(tap the image if the animation does not play)

He clearly travels more than 30 points. (Closer to 120 points.)

Why is this happening, and how can I "reset" the animation correctly so that it does not exceed 30 points?

, , :

  • options: UIViewAnimationOptions.beginFromCurrentState. .
  • , dispatch_after. .
  • "" 0, 0 superview!.layoutIfNeeded.

:

  • , . . 30 15 , 120 .
  • , transform CGAffineTransform.identity CGAffineTransform(scaleX: 0.5, y: 0.5), , 120 .
  • , backgroundColor = UIColor(white: 0, alpha: 0.5) - backgroundColor = UIColor(white: 0, alpha: 0), (.. , , 0.5 )..
+4
2

. removeAllAnimations() , .

@IBAction func didTapButton(_ sender: Any) {
    animatedView.layer.removeAllAnimations()

    centerYConstraint.constant = 0
    view.layoutIfNeeded()

    centerYConstraint.constant = 30
    UIView.animate(withDuration: 2) {
        self.view.layoutIfNeeded()
    }
}

: ( , ), .

+3

, , , reset .

, :

enter image description here
( , )

, , , .

+1

Source: https://habr.com/ru/post/1677819/


All Articles