blob: ef41aecfee09050f5ecd4bcb03031355a7d382be (
plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
//
// SettingsSwitchCell.swift
// MullvadVPN
//
// Created by pronebird on 19/05/2021.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import UIKit
class SettingsSwitchCell: SettingsCell {
private let switchContainer = CustomSwitchContainer()
var action: ((Bool) -> Void)?
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
accessoryView = switchContainer
switchContainer.control.addTarget(
self,
action: #selector(switchValueDidChange),
for: .valueChanged
)
isAccessibilityElement = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setSwitchEnabled(_ isEnabled: Bool) {
switchContainer.isEnabled = isEnabled
}
func setOn(_ isOn: Bool, animated: Bool) {
switchContainer.control.setOn(isOn, animated: animated)
}
override func prepareForReuse() {
super.prepareForReuse()
setSwitchEnabled(true)
}
// MARK: - Actions
@objc private func switchValueDidChange() {
action?(switchContainer.control.isOn)
}
// MARK: - Accessibility
override var accessibilityTraits: UIAccessibilityTraits {
get {
// Use UISwitch traits to make the entire cell behave as "Switch button"
switchContainer.control.accessibilityTraits
}
set {
super.accessibilityTraits = newValue
}
}
override var accessibilityLabel: String? {
get {
titleLabel.text
}
set {
super.accessibilityLabel = newValue
}
}
override var accessibilityValue: String? {
get {
self.switchContainer.control.accessibilityValue
}
set {
super.accessibilityValue = newValue
}
}
override var accessibilityFrame: CGRect {
get {
UIAccessibility.convertToScreenCoordinates(self.bounds, in: self)
}
set {
super.accessibilityFrame = newValue
}
}
override var accessibilityPath: UIBezierPath? {
get {
UIBezierPath(roundedRect: accessibilityFrame, cornerRadius: 4)
}
set {
super.accessibilityPath = newValue
}
}
override func accessibilityActivate() -> Bool {
guard switchContainer.isEnabled else { return false }
let newValue = !switchContainer.control.isOn
setOn(newValue, animated: true)
action?(newValue)
return true
}
}
|