-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom UISlider
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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() | ||
} | ||
|
||
|
||
} |