forked from SwipeCellKit/SwipeCellKit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSwipeTableViewCell+Accessibility.swift
80 lines (63 loc) · 2.71 KB
/
SwipeTableViewCell+Accessibility.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// SwipeTableViewCell+Accessibility.swift
//
// Created by Jeremy Koch
// Copyright © 2017 Jeremy Koch. All rights reserved.
//
import UIKit
extension SwipeTableViewCell {
/// :nodoc:
open override func accessibilityElementCount() -> Int {
guard state != .center else {
return super.accessibilityElementCount()
}
return 1
}
/// :nodoc:
open override func accessibilityElement(at index: Int) -> Any? {
guard state != .center else {
return super.accessibilityElement(at: index)
}
return actionsView
}
/// :nodoc:
open override func index(ofAccessibilityElement element: Any) -> Int {
guard state != .center else {
return super.index(ofAccessibilityElement: element)
}
return element is SwipeActionsView ? 0 : NSNotFound
}
}
extension SwipeTableViewCell {
/// :nodoc:
open override var accessibilityCustomActions: [UIAccessibilityCustomAction]? {
get {
guard let tableView = tableView, let indexPath = tableView.indexPath(for: self) else {
return super.accessibilityCustomActions
}
let leftActions = delegate?.tableView(tableView, editActionsForRowAt: indexPath, for: .left) ?? []
let rightActions = delegate?.tableView(tableView, editActionsForRowAt: indexPath, for: .right) ?? []
let actions = [rightActions.first, leftActions.first].compactMap({ $0 }) + rightActions.dropFirst() + leftActions.dropFirst()
if actions.count > 0 {
return actions.compactMap({ SwipeAccessibilityCustomAction(action: $0,
indexPath: indexPath,
target: self,
selector: #selector(performAccessibilityCustomAction(accessibilityCustomAction:))) })
} else {
return super.accessibilityCustomActions
}
}
set {
super.accessibilityCustomActions = newValue
}
}
@objc func performAccessibilityCustomAction(accessibilityCustomAction: SwipeAccessibilityCustomAction) -> Bool {
guard let tableView = tableView else { return false }
let swipeAction = accessibilityCustomAction.action
swipeAction.handler?(swipeAction, accessibilityCustomAction.indexPath)
if swipeAction.style == .destructive {
tableView.deleteRows(at: [accessibilityCustomAction.indexPath], with: .fade)
}
return true
}
}