This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RepeatingCircle: UIView { | |
let replicatorLayer = CAReplicatorLayer() | |
let circle = CALayer() | |
let duration: TimeInterval = 1.0 | |
let circleSize: CGFloat = 15 | |
let instanceCount = 20 | |
override func draw(_ rect: CGRect) { | |
replicatorLayer.frame = rect | |
replicatorLayer.instanceCount = instanceCount | |
replicatorLayer.instanceDelay = duration / CFTimeInterval(instanceCount) | |
let angle = -CGFloat.pi * 2 / CGFloat(instanceCount) | |
replicatorLayer.instanceTransform = CATransform3DMakeRotation(angle, 0, 0, 1) | |
circle.frame = CGRect(origin: CGPoint.zero, | |
size: CGSize(width: circleSize, height: circleSize)) | |
circle.backgroundColor = UIColor.blue.cgColor | |
circle.cornerRadius = circleSize / 2 | |
replicatorLayer.addSublayer(circle) | |
circle.add(animation: "transform.scale", fromValue: 1, toValue: 0.2, duration: duration) | |
circle.add(animation: "opacity", fromValue: 1, toValue: 0, duration: duration) | |
self.layer.addSublayer(replicatorLayer) | |
} | |
} | |
extension CALayer { | |
func add(animation: String, fromValue: Double, toValue: Double, duration: Double) { | |
let animation = CABasicAnimation(keyPath: animation) | |
animation.fromValue = fromValue | |
animation.toValue = toValue | |
animation.duration = duration | |
animation.repeatCount = Float.greatestFiniteMagnitude | |
self.add(animation, forKey: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment