Skip to content

Instantly share code, notes, and snippets.

@Nexengineer
Created February 13, 2018 09:15
Show Gist options
  • Save Nexengineer/a640f34ae87afc7e2c2dafddee75384e to your computer and use it in GitHub Desktop.
Save Nexengineer/a640f34ae87afc7e2c2dafddee75384e to your computer and use it in GitHub Desktop.
TimeLineView.swift
//
// TimeLineView.swift
// ISTimelineDemo
//
// Created by Neeraj Kumar Mishra on 2/13/18.
// Copyright © 2018 instant:solutions. All rights reserved.
//
import UIKit
class TimeLineView:UIView {
// Variable to Be used
var lineWidth: CGFloat = 2.0
var bubbleRadius:CGFloat = 2.0
var pointDiameter:CGFloat = 8.0
var gapBetweenPoints:CGFloat = 7.0
var lineXPos: CGFloat = 0.0
// Color To be used
var bubbleColor = UIColor.gray
var titleColor:UIColor = .gray
// Method For drawing the Time Line
func drawRectView(_ arrPoints:[String]) {
var intialY:CGFloat = 0.0
lineXPos = (self.frame.width / 3) * 2
for (index, item) in arrPoints.enumerated() {
drawLbl(CGRect(x: 0, y: intialY - pointDiameter/2 + lineWidth, width: (self.frame.width / 3)*2 , height: 13), title: item)
if (index < arrPoints.count - 1) {
drawLine(CGPoint(x: lineXPos + pointDiameter/2, y: intialY ), end: CGPoint(x: lineXPos + pointDiameter/2, y: intialY + gapBetweenPoints), color: bubbleColor.cgColor)
}
drawPoint(CGPoint(x: lineXPos, y:intialY), color: bubbleColor.cgColor, fill: true)
intialY = intialY + gapBetweenPoints
}
}
// Method for drawing Line
fileprivate func drawLine(_ start:CGPoint, end:CGPoint, color:CGColor) {
let path = UIBezierPath()
path.move(to: start)
path.addLine(to: end)
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = color
shapeLayer.lineWidth = lineWidth
self.layer.addSublayer(shapeLayer)
}
// Method for drawing Points in between
fileprivate func drawPoint(_ point:CGPoint, color:CGColor, fill:Bool) {
let path = UIBezierPath(ovalIn: CGRect(x: point.x , y: point.y, width: pointDiameter, height: pointDiameter))
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = color
shapeLayer.fillColor = UIColor.white.cgColor
shapeLayer.lineWidth = lineWidth
self.layer.addSublayer(shapeLayer)
}
// Method for adding labels
fileprivate func drawLbl(_ rect:CGRect, title:String) {
let titleLabel = UILabel(frame: rect)
titleLabel.textColor = titleColor
titleLabel.font = UIFont.boldSystemFont(ofSize: 12)
titleLabel.text = title
titleLabel.textAlignment = .center
self.bringSubview(toFront: titleLabel)
self.addSubview(titleLabel)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment