Skip to content

Instantly share code, notes, and snippets.

@madson
Created December 16, 2016 17:56
Show Gist options
  • Save madson/a0c13e1e44ba3e6685496182fafc33c6 to your computer and use it in GitHub Desktop.
Save madson/a0c13e1e44ba3e6685496182fafc33c6 to your computer and use it in GitHub Desktop.
UIView with a rounded progress border
class ProgressBubble : UIView {
var startAngle: Double = 90
var lineWidth: CGFloat = 4
var timer: Timer!
var borderColor: UIColor = UIColor(colorLiteralRed: 248/255.0, green: 141/255.0, blue: 2/255.0, alpha: 1)
override func draw(_ rect: CGRect) {
self.drawBorder(rect, self.startAngle, self.borderColor)
}
fileprivate func drawBorder(_ frame: CGRect, _ startAngle: Double, _ borderColor: UIColor) {
let ovalRect: CGRect = CGRect(x: frame.minX + self.lineWidth/2,
y: frame.minY + self.lineWidth/2,
width: frame.size.width - self.lineWidth,
height: frame.size.height - self.lineWidth)
let center = CGPoint(x: ovalRect.midX,
y: ovalRect.midY)
let ovalPath = UIBezierPath()
let start: CGFloat = CGFloat(Double(-90.0) * (M_PI / Double(180.0)))
let end: CGFloat = CGFloat(Double(-startAngle) * (M_PI / Double(180.0)))
ovalPath.addArc(withCenter: center, radius: ovalRect.width/2, startAngle: start, endAngle: end, clockwise: true)
ovalPath.lineWidth = self.lineWidth
borderColor.setStroke()
ovalPath.stroke()
}
func startProgress() {
let selector = #selector(self.fillBorder)
self.timer = Timer.scheduledTimer(timeInterval: 0.05,
target: self,
selector: selector,
userInfo: nil,
repeats: true)
}
func stopProgress() {
if self.timer.isValid {
self.timer.invalidate()
}
}
@objc fileprivate func fillBorder() {
if self.startAngle <= -270 {
self.timer.invalidate()
self.startAngle = 90
return
}
self.startAngle = self.startAngle - 10
self.setNeedsDisplay()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment