Last active
January 5, 2018 12:08
-
-
Save eshwartm/6f8dc882d5ff09f036cb51650028a9d9 to your computer and use it in GitHub Desktop.
Simple infinite spinner or loader in Swift
This file contains hidden or 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
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