@@ -22,7 +22,7 @@ open class TableViewIndex : UIControl {
22
22
/// Delegate for the table index object. See TableViewIndexDelegate protocol for details.
23
23
@IBOutlet public weak var delegate : TableViewIndexDelegate ?
24
24
25
- /// Background view is displayed below the index items and can customized with any UIView.
25
+ /// Background view is displayed below the index items and can be customized with any UIView.
26
26
/// If not set or set to nil, creates a default view which mimics the system index appearance.
27
27
public var backgroundView : UIView ! {
28
28
didSet {
@@ -117,6 +117,11 @@ open class TableViewIndex : UIControl {
117
117
118
118
isExclusiveTouch = true
119
119
isMultipleTouchEnabled = false
120
+ isAccessibilityElement = true
121
+ accessibilityTraits = UIAccessibilityTraitAdjustable
122
+ accessibilityLabel = NSLocalizedString ( " Table index " , comment: " Accessibility title for the section index control " )
123
+
124
+ updateAccessibilityValue ( )
120
125
}
121
126
122
127
// MARK: - Updates
@@ -155,6 +160,41 @@ open class TableViewIndex : UIControl {
155
160
item. applyAttributes ( style)
156
161
}
157
162
}
163
+
164
+ private func selectIndex( _ index: Int ) {
165
+ guard index != currentIndex else { return }
166
+
167
+ currentIndex = index
168
+
169
+ updateAccessibilityValue ( )
170
+
171
+ if let delegate = self . delegate
172
+ , delegate. responds ( to: #selector( TableViewIndexDelegate . tableViewIndex ( _: didSelect: at: ) ) ) {
173
+
174
+ let shouldProduceFeedback = delegate. tableViewIndex!( self , didSelect: items [ index] , at: index)
175
+ if shouldProduceFeedback {
176
+ notifyFeedbackGenerator ( )
177
+ }
178
+ }
179
+ }
180
+
181
+ private func updateAccessibilityValue( ) {
182
+ guard currentIndex >= 0 && currentIndex < items. count else { return }
183
+
184
+ let currentItem = items [ currentIndex]
185
+
186
+ let titleText : String
187
+ if let labelText = currentItem. accessibilityLabel {
188
+ titleText = labelText
189
+ } else if let labelText = ( currentItem as? UILabel ) ? . text {
190
+ titleText = labelText
191
+ } else {
192
+ titleText = String . localizedStringWithFormat ( NSLocalizedString ( " Section %d " , comment: " Accessibility title for a numbered section " ) , currentIndex + 1 )
193
+ }
194
+
195
+ let selectedText = NSLocalizedString ( " Selected " , comment: " Accessibility title for the selected state " )
196
+ accessibilityValue = " \( titleText) , \( selectedText) "
197
+ }
158
198
159
199
// MARK: - Layout
160
200
@@ -236,7 +276,7 @@ open class TableViewIndex : UIControl {
236
276
// MARK: - Touches
237
277
238
278
private var currentTouch : UITouch ?
239
- private var currentIndex : Int ?
279
+ private var currentIndex : Int = 0
240
280
241
281
override open func touchesBegan( _ touches: Set < UITouch > , with event: UIEvent ? ) {
242
282
if let touch = touches. first , bounds. contains ( touch. location ( in: self ) ) {
@@ -284,21 +324,12 @@ open class TableViewIndex : UIControl {
284
324
if idx == currentIndex {
285
325
return
286
326
}
287
- currentIndex = idx
288
-
289
- if let delegate = self . delegate
290
- , delegate. responds ( to: #selector( TableViewIndexDelegate . tableViewIndex ( _: didSelect: at: ) ) ) {
291
-
292
- let shouldProduceFeedback = delegate. tableViewIndex!( self , didSelect: items [ idx] , at: idx)
293
- if shouldProduceFeedback {
294
- notifyFeedbackGenerator ( )
295
- }
296
- }
327
+
328
+ selectIndex ( idx)
297
329
}
298
330
299
331
private func finalizeTouch( ) {
300
332
currentTouch = nil
301
- currentIndex = nil
302
333
isHighlighted = false
303
334
cleanupFeedbackGenerator ( )
304
335
}
@@ -332,6 +363,22 @@ open class TableViewIndex : UIControl {
332
363
feedbackGeneratorInstance = nil
333
364
}
334
365
}
366
+
367
+ // MARK: - Accessibility support
368
+
369
+ open override func accessibilityIncrement( ) {
370
+ let newIndex = currentIndex - 1
371
+ if newIndex >= 0 {
372
+ selectIndex ( newIndex)
373
+ }
374
+ }
375
+
376
+ open override func accessibilityDecrement( ) {
377
+ let newIndex = currentIndex + 1
378
+ if newIndex < items. count {
379
+ selectIndex ( newIndex)
380
+ }
381
+ }
335
382
}
336
383
337
384
// MARK: - Protocols
0 commit comments