|
@@ -19,6 +19,10 @@ open class PiPWindow: UIWindow {
|
19
|
19
|
|
20
|
20
|
private let dragController: DragGestureController = DragGestureController()
|
21
|
21
|
|
|
22
|
+ /// Used when in PiP mode to enable/disable exit PiP UI
|
|
23
|
+ private var tapGestureRecognizer: UITapGestureRecognizer?
|
|
24
|
+ private var exitPiPButton: UIButton?
|
|
25
|
+
|
22
|
26
|
/// Help out to bubble up the gesture detection outside of the rootVC frame
|
23
|
27
|
open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
|
24
|
28
|
guard let vc = rootViewController else {
|
|
@@ -66,6 +70,11 @@ open class PiPWindow: UIWindow {
|
66
|
70
|
animateRootViewChange()
|
67
|
71
|
dragController.startDragListener(inView: view)
|
68
|
72
|
dragController.insets = dragBoundInsets
|
|
73
|
+
|
|
74
|
+ // add single tap gesture recognition for displaying exit PiP UI
|
|
75
|
+ let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(toggleExitPiP))
|
|
76
|
+ self.tapGestureRecognizer = tapGestureRecognizer
|
|
77
|
+ view.addGestureRecognizer(tapGestureRecognizer)
|
69
|
78
|
}
|
70
|
79
|
|
71
|
80
|
/// Resize the root view to full screen
|
|
@@ -73,6 +82,14 @@ open class PiPWindow: UIWindow {
|
73
|
82
|
isInPiP = false
|
74
|
83
|
animateRootViewChange()
|
75
|
84
|
dragController.stopDragListener()
|
|
85
|
+
|
|
86
|
+ // hide PiP UI
|
|
87
|
+ exitPiPButton?.removeFromSuperview()
|
|
88
|
+ exitPiPButton = nil
|
|
89
|
+
|
|
90
|
+ // remove gesture
|
|
91
|
+ tapGestureRecognizer?.removeTarget(self, action: #selector(toggleExitPiP))
|
|
92
|
+ tapGestureRecognizer = nil
|
76
|
93
|
}
|
77
|
94
|
|
78
|
95
|
/// Stop the dragging gesture of the root view
|
|
@@ -80,6 +97,22 @@ open class PiPWindow: UIWindow {
|
80
|
97
|
dragController.stopDragListener()
|
81
|
98
|
}
|
82
|
99
|
|
|
100
|
+ /// Customize the presentation of exit pip button
|
|
101
|
+ open func configureExitPiPButton(target: Any, action: Selector) -> UIButton {
|
|
102
|
+ let buttonImage = UIImage.init(named: "image-resize", in: Bundle(for: type(of: self)), compatibleWith: nil)
|
|
103
|
+ let button = UIButton(type: .custom)
|
|
104
|
+ let size: CGSize = CGSize(width: 44, height: 44)
|
|
105
|
+ button.setImage(buttonImage, for: .normal)
|
|
106
|
+ button.backgroundColor = .gray
|
|
107
|
+ button.layer.cornerRadius = size.width / 2
|
|
108
|
+ button.frame = CGRect(origin: CGPoint.zero, size: size)
|
|
109
|
+ if let view = rootViewController?.view {
|
|
110
|
+ button.center = view.convert(view.center, from:view.superview)
|
|
111
|
+ }
|
|
112
|
+ button.addTarget(target, action: action, for: .touchUpInside)
|
|
113
|
+ return button
|
|
114
|
+ }
|
|
115
|
+
|
83
|
116
|
// MARK: - Manage presentation switching
|
84
|
117
|
|
85
|
118
|
private func animateRootViewChange() {
|
|
@@ -102,4 +135,27 @@ open class PiPWindow: UIWindow {
|
102
|
135
|
let y: CGFloat = adjustedBounds.maxY - size.height
|
103
|
136
|
return CGRect(x: x, y: y, width: size.width, height: size.height)
|
104
|
137
|
}
|
|
138
|
+
|
|
139
|
+ // MARK: - Exit PiP
|
|
140
|
+
|
|
141
|
+ @objc private func toggleExitPiP() {
|
|
142
|
+ guard let view = rootViewController?.view else { return }
|
|
143
|
+
|
|
144
|
+ if exitPiPButton == nil {
|
|
145
|
+ // show button
|
|
146
|
+ let button = configureExitPiPButton(target: self, action: #selector(exitPiP))
|
|
147
|
+ view.addSubview(button)
|
|
148
|
+ exitPiPButton = button
|
|
149
|
+
|
|
150
|
+ } else {
|
|
151
|
+ // hide button
|
|
152
|
+ exitPiPButton?.removeFromSuperview()
|
|
153
|
+ exitPiPButton = nil
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ }
|
|
157
|
+
|
|
158
|
+ @objc private func exitPiP() {
|
|
159
|
+ goToFullScreen()
|
|
160
|
+ }
|
105
|
161
|
}
|