@@ -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