CABasicAnimation An object that provides basic, single-keyframe animation capabilities for a layer property.

Using init(keyPath:) method of CABasicAnimation you can animated particular the key path of the property.

Properties of CABasicAnimation

var fromValue: Any?

Defines the value the receiver uses to start interpolation.

var toValue: Any?

Defines the value the receiver uses to end interpolation.

var byValue: Any?

Defines the value the receiver uses to perform relative interpolation.

All properties are optional, and no more than two should be non-nil. The object type should match the type of the property being animated.

We can also set duration and repeat count.


Rotate a UIImageView continuously

let timeInterval: CFTimeInterval = 3
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = CGFloat(Double.pi * 2)
rotateAnimation.isRemovedOnCompletion = false
rotateAnimation.duration = timeInterval
imageView.layer.add(rotateAnimation, forKey: nil)

enter image description here

We can also stop this animation using


Your comment