This repository was archived by the owner on Apr 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathScreenshotInvisibleContainer.swift
114 lines (87 loc) · 2.86 KB
/
ScreenshotInvisibleContainer.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
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
113
114
//
// ScreenshotInvisibleContainer.swift
//
//
// Created by Князьков Илья on 01.03.2022.
//
import UIKit
@objc
public class ScreenshotInvisibleContainer: UITextField {
// MARK: - Private Properties
private let hiddenContainerRecognizer = HiddenContainerRecognizer()
@objc
public var hiddenContainer: UIView? {
try? hiddenContainerRecognizer.getHiddenContainer(from: self)
}
// MARK: - Internal Properties
/// - View, which will be hidden on screenshots and screen recording
private(set) var content: UIView
// MARK: - Initialization
@objc
public init(content: UIView) {
self.content = content
super.init(frame: .zero)
setupInitialState()
}
@objc
public required init?(coder: NSCoder) {
self.content = UIView()
super.init(coder: coder)
setupInitialState()
}
// MARK: - UIView
override public var canBecomeFocused: Bool {
false
}
override public var canBecomeFirstResponder: Bool {
false
}
override public func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return hiddenContainer?.hitTest(point, with: event)
}
override public func layoutSubviews() {
super.layoutSubviews()
isUserInteractionEnabled = content.isUserInteractionEnabled
}
// MARK: - Private methods
private func setupInitialState() {
appendContent(to: hiddenContainer)
backgroundColor = .clear
isUserInteractionEnabled = content.isUserInteractionEnabled
}
private func activateLayoutConstraintsOfContent(to view: UIView) {
[
content.topAnchor.constraint(equalTo: view.topAnchor),
content.bottomAnchor.constraint(equalTo: view.bottomAnchor),
content.leftAnchor.constraint(equalTo: view.leftAnchor),
content.rightAnchor.constraint(equalTo: view.rightAnchor)
].forEach { $0.isActive = true }
}
private func appendContent(to view: UIView?) {
guard let view = view else {
return
}
view.addSubview(content)
view.isUserInteractionEnabled = true
content.translatesAutoresizingMaskIntoConstraints = false
activateLayoutConstraintsOfContent(to: view)
}
}
// MARK: - ScreenshotInvisibleContainerProtocol
@objc
extension ScreenshotInvisibleContainer: ScreenshotInvisibleContainerProtocol {
@objc
public func eraseOldAndAddNewContent(_ newContent: UIView) {
content.removeFromSuperview()
content = newContent
appendContent(to: hiddenContainer)
}
@objc
public func setupContainerAsHideContentInScreenshots() {
isSecureTextEntry = true
}
@objc
public func setupContainerAsDisplayContentInScreenshots() {
isSecureTextEntry = false
}
}