blob: e5424c427ebdd3e520eb719a34c7da1cf9524e98 (
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
|
//
// CustomSwitchContainer.swift
// MullvadVPN
//
// Created by pronebird on 20/05/2021.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import UIKit
class CustomSwitchContainer: UIView {
static let borderEdgeInsets = UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3)
private let borderShape: CAShapeLayer = {
let shapeLayer = CAShapeLayer()
shapeLayer.borderColor = UIColor.Switch.borderColor.cgColor
shapeLayer.borderWidth = 2
shapeLayer.cornerCurve = .continuous
return shapeLayer
}()
let control = CustomSwitch()
var isEnabled: Bool {
get {
control.isEnabled
}
set {
control.isEnabled = newValue
updateBorderOpacity()
}
}
override var accessibilityIdentifier: String? {
didSet {
control.accessibilityIdentifier = accessibilityIdentifier
}
}
override var intrinsicContentSize: CGSize {
controlSize()
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
controlSize()
}
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(control)
layer.addSublayer(borderShape)
control.sizeToFit()
sizeToFit()
borderShape.cornerRadius = bounds.height * 0.5
borderShape.frame = bounds
updateBorderOpacity()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
control.frame.origin = CGPoint(x: Self.borderEdgeInsets.left, y: Self.borderEdgeInsets.top)
}
// MARK: - Private
private func controlSize() -> CGSize {
var size = control.bounds.size
size.width += Self.borderEdgeInsets.left + Self.borderEdgeInsets.right
size.height += Self.borderEdgeInsets.top + Self.borderEdgeInsets.bottom
return size
}
private func updateBorderOpacity() {
CATransaction.begin()
CATransaction.setDisableActions(true)
borderShape.opacity = control.isEnabled ? 1 : 0.2
CATransaction.commit()
}
}
|