Last active
May 3, 2016 01:16
-
-
Save funkyboy/9c7e3e06bea0625cb9b0e643e5498af2 to your computer and use it in GitHub Desktop.
Circle view that progressively fills its border
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
| import UIKit | |
| import XCPlayground | |
| let viewWidth: CGFloat = 300 | |
| var end: CGFloat = 0 | |
| let lineWidth: CGFloat = 20 | |
| let circleView = UIView(frame: CGRectMake(0, 0, viewWidth, viewWidth)) | |
| XCPlaygroundPage.currentPage.liveView = circleView | |
| let circleLayer = CAShapeLayer() | |
| let width = circleView.frame.size.width; | |
| let height = circleView.frame.size.height; | |
| circleLayer.bounds = CGRectMake(-lineWidth/2, -lineWidth/2, width, height) | |
| circleLayer.position = CGPointMake(width/2, height/2) | |
| var c = circleView.frame | |
| c.size.width -= lineWidth | |
| c.size.height -= lineWidth | |
| let path = UIBezierPath(roundedRect: c, cornerRadius: viewWidth/2) | |
| circleLayer.path = path.CGPath | |
| circleLayer.strokeEnd = end | |
| circleLayer.lineCap = kCALineCapRound | |
| circleLayer.fillColor = UIColor.clearColor().CGColor | |
| circleLayer.strokeColor = UIColor.blueColor().CGColor | |
| circleLayer.lineWidth = lineWidth | |
| circleView.layer.addSublayer(circleLayer) | |
| class Responder : NSObject { | |
| func tick() { | |
| end += 0.1 | |
| circleLayer.strokeEnd = end | |
| } | |
| } | |
| let responder = Responder() | |
| NSTimer.scheduledTimerWithTimeInterval(1.0, target: responder, selector: #selector(Responder.tick), userInfo: nil, repeats: true) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Preview
