Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start/stop image view rotation animations

I have a start/stop button and an image view which I want to rotate.

When I press the button I want the to start rotating and when I press the button again the image should stop rotating. I am currently using an UIView animation, but I haven't figured out a way to stop the view animations.

I want the image to rotate, but when the animation stops the image shouldn't go back to the starting position, but instead continue the animation.

var isTapped = true

    @IBAction func startStopButtonTapped(_ sender: Any) {
       ruotate()
       isTapped = !isTapped
    }

    func ruotate() {
        if isTapped {
            UIView.animate(withDuration: 5, delay: 0, options: .repeat, animations: { () -> Void in
            self.imageWood.transform =     self.imageWood.transform.rotated(by: CGFloat(M_PI_2))
            }, completion: { finished in
            ruotate()
            })
    }  }

It is my code, but it doesn't work like I aspect.

like image 570
Edoardo Avatar asked Jul 25 '17 14:07

Edoardo


People also ask

How do I turn off infinite animation on Android?

Use clearAnimation() to stop an animation.


2 Answers

Swift 3.x

Start Animation

let rotationAnimation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
    rotationAnimation.toValue = NSNumber(value: .pi * 2.0)
    rotationAnimation.duration = 0.5;
    rotationAnimation.isCumulative = true;
    rotationAnimation.repeatCount = .infinity;
    self.imageWood?.layer.add(rotationAnimation, forKey: "rotationAnimation")

Stop animation

self.imageWood?.layer.removeAnimation(forKey: "rotationAnimation")

Swift 2.x

Start Animation

let rotationAnimation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
    rotationAnimation.toValue = NSNumber(double: M_PI * 2.0)
    rotationAnimation.duration = 1;
    rotationAnimation.cumulative = true;
    rotationAnimation.repeatCount = .infinity;
    self.imageWood?.layer.addAnimation(rotationAnimation, forKey: "rotationAnimation")

Stop animation

self.imageWood?.layer.removeAnimation(forKey: "rotationAnimation")
like image 113
Salman Ghumsani Avatar answered Oct 24 '22 14:10

Salman Ghumsani


If you use UIView Animation the OS still creates one or more CAAnimation objects. Thus, to stop a UIView animation you can still use:

myView.layer.removeAllAnimations()

Or if you create the animation using a CAAnimation on a layer:

myLayer.removeAllAnimations()

In either case, you can capture the current state of the animation and set that as the final state before removing the animation. If you're doing an animation on a view's transform, like in this question, that code might look like this:

func stopAnimationForView(_ myView: UIView) {

  //Get the current transform from the layer's presentation layer
  //(The presentation layer has the state of the "in flight" animation)
  let transform = myView.layer.presentationLayer.transform

  //Set the layer's transform to the current state of the transform
  //from the "in-flight" animation
  myView.layer.transform = transform

  //Now remove the animation 
  //and the view's layer will keep the current rotation
  myView.layer.removeAllAnimations()
}

If you're animating a property other than the transform you'd need to change the code above.

like image 41
Duncan C Avatar answered Oct 24 '22 12:10

Duncan C