Tolgay Toklar
Tolgay Toklar

Reputation: 4343

Spinning imageview animation

I want to spin a imageview for forever. I tried following code:

    UIView.animateWithDuration(3, animations: {
        self.loginLogo.transform = CGAffineTransformMakeRotation((360 * CGFloat(M_PI)) / 360)
        }){ (finished) -> Void in
            self.rotateImage()
    }

But this is working for one time. My imageview is not spinning forever. How can I fix it?

Upvotes: 1

Views: 116

Answers (3)

ImAshwyn
ImAshwyn

Reputation: 26

This should work

func startAnimationWithDuration(duration:CGFloat)
    {
        var rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotationAnimation.fromValue = 0
        rotationAnimation.toValue = 2 * M_PI
        rotationAnimation.duration = NSTimeInterval(duration)
        rotationAnimation.repeatCount = 1000.0 //some large value
        imageView.layer.addAnimation(rotationAnimation, forKey: "spin")
    }

Upvotes: 0

Ankit Sachan
Ankit Sachan

Reputation: 7840

use this on layer of your view

var myImageView:UIImageView?
    var rotation = CABasicAnimation(keyPath: "transform.rotation")
    rotation.fromValue = 0.0
    rotation.toValue = 2*M_PI
    rotation.duration = 1.1
    rotation.repeatCount = Float.infinity
    myImageView?.layer.addAnimation(rotation, forKey: "Spin")

Upvotes: 0

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71854

If you want to rotate your image forever you can do it this way:

func rotateViewLayer() {
    let rotateView = CABasicAnimation()

    rotateView.fromValue = 0.degreesToRadian
    rotateView.toValue = 360.degreesToRadian
    rotateView.duration = 1
    rotateView.repeatCount = Float.infinity
    rotateView.removedOnCompletion = false
    rotateView.fillMode = kCAFillModeForwards
    rotateView.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
    imageView.layer.addAnimation(rotateView, forKey: "transform.rotation.z")
}

And here is your helper extension:

extension Int {
    var degreesToRadian : CGFloat {
        return CGFloat(self) * CGFloat(M_PI) / 180.0
    }
}

And you can refer THIS sample project for more Info.

Upvotes: 1

Related Questions