Skip to content

Commit 4d150e1

Browse files
committed
added example of using two knobs
1 parent fed9766 commit 4d150e1

File tree

3 files changed

+62
-13
lines changed

3 files changed

+62
-13
lines changed

KnobView.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88

99
import UIKit
1010

11+
protocol KnobDelegate {
12+
func updateKnobValue(_ value: Double, tag: Int)
13+
}
14+
1115
@IBDesignable
1216
class KnobView: UIView {
17+
18+
var delegate: KnobDelegate?
1319

1420
var minimum = 0.0 {
1521
didSet {
@@ -85,6 +91,8 @@ class KnobView: UIView {
8591

8692
lastX = touchPoint.x
8793
lastY = touchPoint.y
94+
95+
delegate?.updateKnobValue(value, tag: self.tag)
8896
}
8997

9098
}

SynthUISpike/Base.lproj/Main.storyboard

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,33 @@
2121
<rect key="frame" x="0.0" y="0.0" width="1024" height="337"/>
2222
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2323
<subviews>
24-
<view contentMode="center" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="DJU-Fx-KsR" customClass="KnobView" customModule="SynthUISpike" customModuleProvider="target">
25-
<rect key="frame" x="175" y="103" width="130" height="130"/>
24+
<view tag="100" contentMode="center" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="DJU-Fx-KsR" customClass="KnobView" customModule="SynthUISpike" customModuleProvider="target">
25+
<rect key="frame" x="155" y="103" width="130" height="130"/>
2626
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
2727
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
2828
</view>
29+
<view tag="101" contentMode="center" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="7A7-28-Zh2" customClass="KnobView" customModule="SynthUISpike" customModuleProvider="target">
30+
<rect key="frame" x="354" y="113" width="110" height="110"/>
31+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
32+
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
33+
</view>
34+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Knob Value: " textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="iCj-20-bn8">
35+
<rect key="frame" x="231" y="38" width="179" height="21"/>
36+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
37+
<fontDescription key="fontDescription" name="Helvetica" family="Helvetica" pointSize="20"/>
38+
<color key="textColor" red="0.90196078431372551" green="0.53333333333333333" blue="0.0078431372549019607" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
39+
<nil key="highlightedColor"/>
40+
</label>
2941
</subviews>
3042
<color key="backgroundColor" red="0.21176470588235294" green="0.20784313725490194" blue="0.21568627450980393" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
3143
</view>
3244
<value key="contentSizeForViewInPopover" type="size" width="1024" height="337"/>
3345
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
3446
<size key="freeformSize" width="1024" height="337"/>
3547
<connections>
36-
<outlet property="myKnob" destination="DJU-Fx-KsR" id="g8M-7f-yJE"/>
48+
<outlet property="displayLabel" destination="iCj-20-bn8" id="UtE-vd-65M"/>
49+
<outlet property="knobOne" destination="DJU-Fx-KsR" id="R5h-tc-9Hx"/>
50+
<outlet property="knobTwo" destination="7A7-28-Zh2" id="cJr-dX-jRB"/>
3751
</connections>
3852
</viewController>
3953
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>

SynthUISpike/ViewController.swift

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,49 @@ import UIKit
1010

1111
class ViewController: UIViewController {
1212

13-
@IBOutlet weak var myKnob: KnobView!
13+
@IBOutlet weak var knobOne: KnobView!
14+
@IBOutlet weak var knobTwo: KnobView!
15+
@IBOutlet weak var displayLabel: UILabel!
16+
17+
enum ControlTag: Int {
18+
case control1 = 100
19+
case control2 = 101
20+
}
1421

1522
override func viewDidLoad() {
1623
super.viewDidLoad()
1724
// Do any additional setup after loading the view, typically from a nib.
25+
26+
knobOne.delegate = self
27+
knobTwo.delegate = self
1828
}
1929

20-
override func didReceiveMemoryWarning() {
21-
super.didReceiveMemoryWarning()
22-
// Dispose of any resources that can be recreated.
23-
}
30+
31+
}
2432

25-
@IBAction func mySlider(_ sender: UISlider) {
26-
//myKnob.knobScale = CGFloat(sender.value) + 0.8
27-
myKnob.value = Double(sender.value)
28-
}
2933

30-
}
34+
//*****************************************************************
35+
// MARK: - 🎛 Knob Delegate
36+
//*****************************************************************
3137

38+
extension ViewController: KnobDelegate {
39+
40+
func updateKnobValue(_ value: Double, tag: Int) {
41+
42+
switch (tag) {
43+
44+
// VCOs
45+
case ControlTag.control1.rawValue:
46+
let intValue = Int(floor(value * 10))
47+
let message = "Knob One: \(intValue)"
48+
displayLabel.text = message
49+
50+
case ControlTag.control2.rawValue:
51+
let message = "Knob Two: \(String(format: "%.02f", value))"
52+
displayLabel.text = message
53+
54+
default:
55+
break
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)