Skip to content

Commit 47caf0e

Browse files
author
RYUHEI KAMINISHI
committed
🎨 Refactoring
1 parent bc9e781 commit 47caf0e

2 files changed

Lines changed: 69 additions & 44 deletions

File tree

‎Example/Example/ViewController.swift‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@
99
import UIKit
1010
import FloatingSegmentedControl
1111

12-
class ViewController: UIViewController {
12+
final class ViewController: UIViewController {
1313

14-
@IBOutlet weak var control: FloatingSegmentedControl!
14+
@IBOutlet private weak var control: FloatingSegmentedControl!
1515

1616
override func viewDidLoad() {
1717
super.viewDidLoad()
18-
// Do any additional setup after loading the view.
18+
1919
control.setSegments(with: [
2020
"Today", "Days", "Months"
2121
])
22-
control.target = self
23-
control.action = #selector(update(_:))
22+
control.addTarget(self, action: #selector(update(_:)))
2423
control.isAnimateFocusMoving = true
2524
}
2625

‎Source/FloatingSegmentedControl.swift‎

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,6 @@ open class FloatingSegmentedControl: UIView, NibInstantiatable {
4545
setUp()
4646
}
4747

48-
private func setUp() {
49-
loadNib()
50-
}
51-
52-
private func loadNib() {
53-
let bundle = Bundle(for: type(of: self))
54-
let view = FloatingSegmentedControl.fromNib(inBundle: bundle, filesOwner: self)
55-
addSubview(view)
56-
57-
view.translatesAutoresizingMaskIntoConstraints = false
58-
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[view]-0-|",
59-
options: NSLayoutConstraint.FormatOptions(rawValue: 0),
60-
metrics: nil,
61-
views: ["view": view]))
62-
63-
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[view]-0-|",
64-
options: NSLayoutConstraint.FormatOptions(rawValue: 0),
65-
metrics: nil,
66-
views: ["view" : view]))
67-
}
68-
6948
open override func awakeFromNib() {
7049
super.awakeFromNib()
7150

@@ -99,6 +78,67 @@ open class FloatingSegmentedControl: UIView, NibInstantiatable {
9978
updateFocusSegment()
10079
}
10180

81+
/// Add segment elements
82+
/// - Parameter titles: Segment titles
83+
public func setSegments(with titles: [String]) {
84+
removeAllSegments()
85+
titles.forEach {
86+
let bundle = Bundle(for: FloatingSegmentedControl.self)
87+
let segment: FloatingSegment = FloatingSegment.fromNib(inBundle: bundle, filesOwner: nil)
88+
segment.floatingSegmentedControl = self
89+
segment.title = $0
90+
stackView.addArrangedSubview(segment)
91+
}
92+
}
93+
94+
/// Select segment from code
95+
/// - Parameter index: Destination index
96+
public func move(to index: Int) {
97+
if index < segments.count {
98+
focusedIndex = index
99+
sendAction()
100+
} else {
101+
print("index \(index) is out of index")
102+
}
103+
}
104+
105+
/// Set a function to be called when the button is pressed
106+
/// - Parameter target: Calling class
107+
/// - Parameter selector: The function to call
108+
public func addTarget(_ target: NSObject, action selector: Selector) {
109+
self.target = target
110+
self.action = selector
111+
}
112+
113+
114+
func select(segment: FloatingSegment) {
115+
if let targetIndex = index(of: segment) {
116+
focusedIndex = targetIndex
117+
sendAction()
118+
}
119+
}
120+
121+
private func setUp() {
122+
loadNib()
123+
}
124+
125+
private func loadNib() {
126+
let bundle = Bundle(for: type(of: self))
127+
let view = FloatingSegmentedControl.fromNib(inBundle: bundle, filesOwner: self)
128+
addSubview(view)
129+
130+
view.translatesAutoresizingMaskIntoConstraints = false
131+
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[view]-0-|",
132+
options: NSLayoutConstraint.FormatOptions(rawValue: 0),
133+
metrics: nil,
134+
views: ["view": view]))
135+
136+
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[view]-0-|",
137+
options: NSLayoutConstraint.FormatOptions(rawValue: 0),
138+
metrics: nil,
139+
views: ["view" : view]))
140+
}
141+
102142
private func updateFocusSegment() {
103143
knob.layer.cornerRadius = backgroundView.layer.cornerRadius - (backgroundView.bounds.height - knob.bounds.height) / 2
104144
if focusedIndex < segments.count {
@@ -134,29 +174,15 @@ open class FloatingSegmentedControl: UIView, NibInstantiatable {
134174
return nil
135175
}
136176

137-
public func setSegments(with titles: [String]) {
138-
removeAllSegments()
139-
titles.forEach {
140-
let bundle = Bundle(for: FloatingSegmentedControl.self)
141-
let segment: FloatingSegment = FloatingSegment.fromNib(inBundle: bundle, filesOwner: nil)
142-
segment.floatingSegmentedControl = self
143-
segment.title = $0
144-
stackView.addArrangedSubview(segment)
145-
}
146-
}
147-
148-
func removeAllSegments() {
177+
private func removeAllSegments() {
149178
segments.forEach {
150179
$0.removeFromSuperview()
151180
}
152181
}
153182

154-
func select(segment: FloatingSegment) {
155-
if let targetIndex = index(of: segment) {
156-
focusedIndex = targetIndex
157-
if let action = action {
158-
UIApplication.shared.sendAction(action, to: target, from: self, for: nil)
159-
}
183+
private func sendAction() {
184+
if let action = action {
185+
UIApplication.shared.sendAction(action, to: target, from: self, for: nil)
160186
}
161187
}
162188
}

0 commit comments

Comments
 (0)