PIPWKit
用于 iOS 的画中画 窗口
准备就绪的
- 设备方向
- iOS11+ 且支持 iOS13 模态样式
- Swift 5.x
- XCode 11.5
- 覆盖模态上下文
如果您的项目是 IOS13+,您必须在 show 函数中设置 mainWindow 参数
安装
CocoaPods
PIPWKit 可通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 中
pod 'PIPWKit'
使用
PIPUsable
protocol PIPWUsable {
var initialState: PIPWState { get }
var initialPosition: PIPWPosition { get }
var pipSize: CGSize { get }
func didChangedState(_ state: PIPWState)
}
PIPWKit
open class PIPWKit {
static var isActive: Bool { return floatingWindow != nil }
static var isPIP: Bool { return state == .pip }
static var floatingWindow: PIPWViewWindow?
static var mainWindow: UIWindow?
class func show(with viewController: UIViewController, mainWindow: UIWindow? = nil, completion: (() -> Void)? = nil) { ... }
class func dismiss(animated: Bool, completion: (() -> Void)? = nil) { ... }
}
PIPWViewWindow: UIViewController, PIPWUsable
func setNeedUpdatePIPSize()
func startPIPMode()
func stopPIPMode()
At a Glance
显示 & 关闭
class PIPViewController: UIViewController, PIPWUsable {
var initialState: PIPWState { return .pip }
var pipSize: CGSize { return CGSize(width: 200.0, height: 200.0) }
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .blue
view.layer.borderColor = UIColor.red.cgColor
view.layer.borderWidth = 1.0
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
if PIPWKit.isPIP {
PIPWKit.floatingWindow?.stopPIPMode()
} else {
PIPWKit.floatingWindow?.startPIPMode()
}
}
func didChangedState(_ state: PIPWState) {
switch state {
case .pip:
print("PIPViewController.pip")
case .full:
print("PIPViewController.full")
}
}
}
let vc = PIPViewController()
PIPWKit.show(with: vc)
PIPWKit.dismiss(animated: true)
更新 PIP 大小
class PIPViewController: UIViewController, PIPWUsable {
func onUpdatePIPSize(_ sender: UIButton) {
pipSize = CGSize(width: 100 + Int(arc4random_uniform(100)),
height: 100 + Int(arc4random_uniform(100)))
PIPWKit.floatingWindow?.setNeedUpdatePIPSize()
}
}
全屏 <-> PIP 模式
class PIPViewController: UIViewController, PIPWUsable {
func fullScreenAndPIPMode() {
if PIPWKit.isPIP {
PIPWKit.floatingWindow?.stopPIPMode()
} else {
PIPWKit.floatingWindow?.startPIPMode()
}
}
func didChangedState(_ state: PIPWState) {}
}
作者
PIPWKit 由 Daniele Galiotto (gali8) 开发,他是 Nexor Technology 的首席信息官。
PIPWKit 受启发于 PIPKit,由 Taeun Kim (kofktu) 创建,https://github.com/Kofktu/PIPKit
许可协议
PIPWKit(类似于PIPKit)在MIT许可下提供。有关更多信息,请参阅LICENSE
文件。