Skip to content

Instantly share code, notes, and snippets.

@Bashta
Created July 25, 2016 03:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bashta/8b3bab64a579cbdaaac9550e1c250b63 to your computer and use it in GitHub Desktop.
Save Bashta/8b3bab64a579cbdaaac9550e1c250b63 to your computer and use it in GitHub Desktop.
//
// PortionProgress.swift
// PortionProgress
//
// Created by Erison Veshi on 7/24/16.
// Copyright © 2016 Bashta. All rights reserved.
//
import UIKit
@IBDesignable
public final class ProportionProgressView: UIView {
@IBInspectable public var widthRatio: CGFloat = 0.14
@IBInspectable public var heightRatio: CGFloat = 0.2
@IBInspectable public var progressPercentage: CGFloat = 0.5 {
didSet {
// self.setNeedsDisplay()
}
}
@IBInspectable public var trackTint: UIColor = UIColor.grayColor()
@IBInspectable public var trackWidth: CGFloat = 1
public var trackCapStyle: CGLineCap = .Round
@IBInspectable public var progressTint: UIColor = UIColor.orangeColor()
@IBInspectable public var progressWidth: CGFloat = 1
public var progressCapStyle: CGLineCap = .Round
public override func drawRect(rect: CGRect) {
drawTrackArcInRect(rect)
drawProgressArcInRect(rect)
drawPropertionRectangleInRect(rect)
}
}
private extension ProportionProgressView {
//MARK:- Drawing Helpers
func drawTrackArcInRect(rect: CGRect) {
let positionX = CGRectGetWidth(rect) / 2
let positionY = (CGRectGetHeight(rect) / 2) * heightRatio
let startAngle = -CGFloat(M_PI_2)
let endAngle = startAngle + CGFloat(2 * M_PI)
let trackArc = UIBezierPath(arcCenter: CGPoint(x: positionX, y: positionY),
radius: (CGRectGetWidth(rect) / 2) * heightRatio,
startAngle: startAngle,
endAngle: endAngle,
clockwise: true)
trackArc.lineCapStyle = trackCapStyle
trackArc.lineWidth = trackWidth
trackTint.setStroke()
trackArc.stroke()
}
func drawProgressArcInRect(rect: CGRect) {
let positionX = CGRectGetWidth(rect) / 2
let positionY = (CGRectGetHeight(rect) / 2) * heightRatio
let progressAngle = CGFloat(progressPercentage) * CGFloat(2 * M_PI)
let startAngle = -CGFloat(M_PI_2)
let endAngle = startAngle + progressAngle
let progressArc = UIBezierPath(arcCenter: CGPoint(x: positionX, y: positionY),
radius: (CGRectGetWidth(rect) / 2) * heightRatio,
startAngle: startAngle,
endAngle: endAngle,
clockwise: true)
progressArc.lineCapStyle = progressCapStyle
progressArc.lineWidth = progressWidth
progressTint.setStroke()
progressArc.stroke()
}
func drawPropertionRectangleInRect(rect: CGRect) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment