-
Notifications
You must be signed in to change notification settings - Fork 9
/
ImageKnob.swift
78 lines (66 loc) · 1.77 KB
/
ImageKnob.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// ImageKnob.swift
// UniversalKnob
//
// Created by Matthew Fecher on 10/19/17.
// Copyright © 2017 Matthew Fecher. All rights reserved.
//
import UIKit
@IBDesignable
public class ImageKnob: Knob {
@IBInspectable open var totalFrames: Int = 0 {
didSet {
createImageArray()
}
}
@IBInspectable open var imageName: String = "knob01_" {
didSet {
createImageArray()
}
}
var imageView = UIImageView()
var imageArray = [UIImage]()
var currentFrame: Int {
return Int(Double(knobValue) * Double(totalFrames))
}
public override func layoutSubviews() {
super.layoutSubviews()
imageView.frame = CGRect(
x: 0,
y: 0,
width: self.bounds.width,
height: self.bounds.height)
}
public override func draw(_ rect: CGRect) {
super.draw(rect)
if imageArray.indices.contains(currentFrame) {
imageView.image = imageArray[currentFrame]
}
}
// Init / Lifecycle
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
createImageArray()
addSubview(imageView)
}
// Create Image Array
func createImageArray() {
imageArray.removeAll()
for i in 0..<totalFrames {
guard let image = UIImage(
named: "\(imageName)\(i)",
in: Bundle(for: type(of: self)),
compatibleWith: traitCollection)
else { continue }
imageArray.append(image)
}
imageView.image = UIImage(named: "\(imageName)\(currentFrame)")
}
}