Skip to content

Commit 44b7388

Browse files
committed
Delegate view creation back to SMRotaryProtocol
1 parent ce122c2 commit 44b7388

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

RotatingWheelController/ViewController.swift

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,18 @@ import os
1010
import UIKit
1111

1212
class ViewController: UIViewController, SMRotaryProtocol {
13+
func viewFor(tag: Int) -> UIView {
14+
let view = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 40))
15+
view.backgroundColor = .red
16+
view.tag = tag
17+
view.text = "\(tag)"
18+
return view
19+
}
20+
1321
func wheelDidChangeValue(to: Int) {
1422
os_log(OSLogType.default, "%d", to);
1523
}
16-
24+
1725
override func viewDidLoad() {
1826
super.viewDidLoad()
1927
let wheel = SMRotaryWheel(frame: view.frame, delegate: self, numberOfSections: 3)

SMRotaryProtocol.swift

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// Copyright © 2018 tom. All rights reserved.
77
//
88

9+
import UIKit
10+
911
protocol SMRotaryProtocol {
1012
func wheelDidChangeValue(to: Int)
13+
func viewFor(tag: Int) -> UIView
1114
}

SMRotaryWheel.swift

+3-11
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,10 @@ class SMRotaryWheel: UIControl {
8787
let dy = point.y - centre.y
8888
return (dx * dx + dy * dy).squareRoot()
8989
}
90-
91-
func createView(index: Int) -> UIView {
92-
let view = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 40))
93-
view.backgroundColor = .red
94-
view.tag = index
95-
view.text = "\(index)"
96-
return view
97-
}
9890

9991
func setup() {
10092
container = UIView(frame: frame)
101-
guard let container = container, let numberOfSections = numberOfSections else {
93+
guard let container = container, let delegate = delegate, let numberOfSections = numberOfSections else {
10294
return
10395
}
10496

@@ -107,7 +99,7 @@ class SMRotaryWheel: UIControl {
10799

108100
for i in 0..<numberOfSections {
109101
// Create and add view to container
110-
let view = createView(index: i)
102+
let view = delegate.viewFor(tag: i)
111103
view.layer.anchorPoint = CGPoint(x: 1.0, y: 0.5)
112104
view.layer.position = CGPoint(x: container.bounds.size.width / 2.0, y: container.bounds.size.height / 2.0)
113105
view.transform = CGAffineTransform(rotationAngle: CGFloat(i) * sectorAngle)
@@ -132,6 +124,6 @@ class SMRotaryWheel: UIControl {
132124
container.isUserInteractionEnabled = false
133125
addSubview(container)
134126

135-
delegate?.wheelDidChangeValue(to: currentSector)
127+
delegate.wheelDidChangeValue(to: currentSector)
136128
}
137129
}

0 commit comments

Comments
 (0)