From 8428dd95c275a660e65a3724e7582b414124e89e Mon Sep 17 00:00:00 2001 From: Daniel Ornelas Date: Fri, 9 Mar 2018 11:59:15 -0600 Subject: [PATCH] Implement Exit PiP mode in PiPWindow --- ios/sdk/image-resize@2x.png | Bin 0 -> 509 bytes ios/sdk/image-resize@3x.png | Bin 0 -> 724 bytes ios/sdk/sdk.xcodeproj/project.pbxproj | 12 +++- .../PiPWindow/PiPWindow.swift | 56 ++++++++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 ios/sdk/image-resize@2x.png create mode 100644 ios/sdk/image-resize@3x.png diff --git a/ios/sdk/image-resize@2x.png b/ios/sdk/image-resize@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..d66dd8a3f4b98d73c4eb6682f3a84b0f71ca194b GIT binary patch literal 509 zcmVPx$w@E}nRA>e5nN3atK@f#$7r4`FCFN*ZjSsw4#gge8|2KLNq`!I=fXCY zfD3EE+5El5Q&YeSP@fikmPvl?^$+B)T3w2K9*Zo$0UF1^fajZ_w|u;bge3 zJZg}TIbW;N2gDWf3&}%;uL3ysrl7vzT4bfp8!k_&o;>mCh821X1nk00000NkvXXu0mjf7AE9+ literal 0 HcmV?d00001 diff --git a/ios/sdk/image-resize@3x.png b/ios/sdk/image-resize@3x.png new file mode 100644 index 0000000000000000000000000000000000000000..7a4244fec35c7abf11ef86cccfe5bb8566ff49d3 GIT binary patch literal 724 zcmV;_0xSKAP)Px%j!8s8RCodH+p$i=Fc1ddhM{96R+f%D1*{A_L1FDX@CpSaMqYqdfLEX}z*vcq zp@j{Ju2ki}Dybsl#OEZHxbs;$HF2CY_hFkhO?o{mCSU?4U;-v!0w!PrCZI)NyLDjtsX%v}Bm>kGCkL8if49ul3^8OD zHRrgAV}IY}k%`+*+eMw?RI!1AUZILhceQm|nA(;zAU8%(pmvHO} zL*~9lXXiK)u8F)?pgy7gB+IhzitTAsqxTFEUrsNgIcf7DFb#!3mcSSyZV?X%nv--) zuz|d7vk91h37CKhn1CJue117X(AR?{;stT4C%cxJ0AqysLAb?`AX0@xSg$SZW$zxq zA)uw~3|pK%1BZa7E#5-zu1p-7laA$PnV&h5h`MAMfpaDvhY~=-?2F#X;p9N9pG}g) z$$^mj-$L7pf~|G`%8Q+8&e1Wfip1%)$j*H&+MqsNoDfh1&Zq!Y_v-eX(mJ>2=*6k) zL4fjow<`H2dA;4 zLtvPV5%fSTJs2BPblnrUBVRkL37CKhn1BhGfC-p@Fo9pY?CkuE!s)C40000 Bool { guard let vc = rootViewController else { @@ -66,6 +70,11 @@ open class PiPWindow: UIWindow { animateRootViewChange() dragController.startDragListener(inView: view) dragController.insets = dragBoundInsets + + // add single tap gesture recognition for displaying exit PiP UI + let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(toggleExitPiP)) + self.tapGestureRecognizer = tapGestureRecognizer + view.addGestureRecognizer(tapGestureRecognizer) } /// Resize the root view to full screen @@ -73,6 +82,14 @@ open class PiPWindow: UIWindow { isInPiP = false animateRootViewChange() dragController.stopDragListener() + + // hide PiP UI + exitPiPButton?.removeFromSuperview() + exitPiPButton = nil + + // remove gesture + tapGestureRecognizer?.removeTarget(self, action: #selector(toggleExitPiP)) + tapGestureRecognizer = nil } /// Stop the dragging gesture of the root view @@ -80,6 +97,22 @@ open class PiPWindow: UIWindow { dragController.stopDragListener() } + /// Customize the presentation of exit pip button + open func configureExitPiPButton(target: Any, action: Selector) -> UIButton { + let buttonImage = UIImage.init(named: "image-resize", in: Bundle(for: type(of: self)), compatibleWith: nil) + let button = UIButton(type: .custom) + let size: CGSize = CGSize(width: 44, height: 44) + button.setImage(buttonImage, for: .normal) + button.backgroundColor = .gray + button.layer.cornerRadius = size.width / 2 + button.frame = CGRect(origin: CGPoint.zero, size: size) + if let view = rootViewController?.view { + button.center = view.convert(view.center, from:view.superview) + } + button.addTarget(target, action: action, for: .touchUpInside) + return button + } + // MARK: - Manage presentation switching private func animateRootViewChange() { @@ -102,4 +135,27 @@ open class PiPWindow: UIWindow { let y: CGFloat = adjustedBounds.maxY - size.height return CGRect(x: x, y: y, width: size.width, height: size.height) } + + // MARK: - Exit PiP + + @objc private func toggleExitPiP() { + guard let view = rootViewController?.view else { return } + + if exitPiPButton == nil { + // show button + let button = configureExitPiPButton(target: self, action: #selector(exitPiP)) + view.addSubview(button) + exitPiPButton = button + + } else { + // hide button + exitPiPButton?.removeFromSuperview() + exitPiPButton = nil + } + + } + + @objc private func exitPiP() { + goToFullScreen() + } }