Skip to content

Instantly share code, notes, and snippets.

@eshwartm
Last active January 5, 2018 12:08
Show Gist options
  • Save eshwartm/6f8dc882d5ff09f036cb51650028a9d9 to your computer and use it in GitHub Desktop.
Save eshwartm/6f8dc882d5ff09f036cb51650028a9d9 to your computer and use it in GitHub Desktop.
Simple infinite spinner or loader in Swift
func drawAndRotateHollowCircle() {
// draw
let circlePath = UIBezierPath(arcCenter: CGPoint(x: 50, y: 50) , radius: CGFloat(10), startAngle: CGFloat(0), endAngle: CGFloat(Double.pi * 2 * 0.75), clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath
if let rect = shapeLayer.path?.boundingBoxOfPath {
shapeLayer.frame = rect
shapeLayer.bounds = rect
}
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = UIColor.gray.cgColor
shapeLayer.lineWidth = 3
view.layer.addSublayer(shapeLayer)
// rotate
let rotationTransform = CATransform3DMakeRotation(.pi, 0, 0, 1)
let rotationAnimation = CABasicAnimation(keyPath: "transform")
rotationAnimation.toValue = NSValue(caTransform3D: rotationTransform)
rotationAnimation.duration = 0.5
rotationAnimation.isCumulative = true
rotationAnimation.repeatCount = HUGE
shapeLayer.add(rotationAnimation, forKey: "transform")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment