Skip to content

Commit

Permalink
Feat: Implememt TickMarkSlider #89
Browse files Browse the repository at this point in the history
Custom UISlider
  • Loading branch information
jinios committed Jul 21, 2019
1 parent ee93a1c commit 77c11cf
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions MartHoliday/MartHoliday/View/TickMarkSlider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// TickMarkSlider.swift
// MartHoliday
//
// Created by YOUTH2 on 21/07/2019.
// Copyright © 2019 JINiOS. All rights reserved.
//

import UIKit

class TickMarkSlider: UISlider {

var numberOfTickMarks: Float?
var unit: Float?

let hapticGenerator = UIImpactFeedbackGenerator(style: .light)

convenience init(tick: Float, maximumValue: Float, frame: CGRect) {
self.init(frame: frame)
self.numberOfTickMarks = tick
self.maximumValue = maximumValue
self.unit = (maximumValue - self.minimumValue) / tick
}

override init(frame: CGRect) {
super.init(frame: frame)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

func addTickMarks() {
let width: CGFloat = 2.0
let height: CGFloat = 15.0
let yPosition: CGFloat = (self.frame.size.height - 13)/2
var xPosition: CGFloat = 0

guard let numberOfTickMarks = self.numberOfTickMarks else { return }
let ratio = Float(self.frame.width) / numberOfTickMarks

for i in 0..<Int(numberOfTickMarks) {
if i == 0 {
xPosition += width
continue
}
xPosition = CGFloat(Float(i) * ratio)

let tick = UIView(frame: CGRect(x: xPosition, y: yPosition, width: width, height: height))
tick.backgroundColor = .gray

self.insertSubview(tick, belowSubview: self)
xPosition += width
}
}

override func endTracking(_ touch: UITouch?, with event: UIEvent?) {
guard let unit = self.unit else { return }
let newStep = roundf(self.value / unit)
self.value = newStep * unit
hapticGenerator.impactOccurred()
}


}

0 comments on commit 77c11cf

Please sign in to comment.