PiPhone 1.0.1

PiPhone 1.0.1

Oleksandr Zhuhan 维护。



PiPhone 1.0.1

  • ky1vstar

PiPhone

Version License Platform

PiPhone 是一个支持 iPhone 设备上画中画功能(用户初始化的浮动的可调整大小的视频窗口的播放)的即插即用解决方案。它设计得尽可能模仿默认的 AVPictureInPictureController 行为。

概述

功能

  • 支持默认不支持画中画功能的设备
  • 如果已经配置了 AVPictureInPictureController,则无需额外工作
  • 外观与默认的相同
  • 处理视频大小变化
  • 处理视频错误
  • 支持触摸、双击、捏合和拖动手势
  • 模拟 AVPictureInPictureControllerDelegate 的行为

要求

  • Xcode 8.0+
  • iOS 9.0+

安装

PiPhone 可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中:

pod 'PiPhone'

使用方法

要在画中画模式下显示视频,您可以同时使用 AVPlayerViewController 和任何配置为可显示在画中画的自定义视频播放器 已配置的可显示在画中画

基本上,您不需要执行任何额外的操作,您的视频播放器现在支持画中画!但 PiPhone 提供了一些您可以找到有用的自定义选项。

请确保导入框架头文件:Objective-C 中的 #import <PiPhone/PiPhone.h> 或 Swift 中的 import PiPhone

内容内边距调整行为

默认的 AVPictureInPictureController 行为是找到最顶层的 UINavigationController 的栏,UITabBarController 的栏,safeAreaInsets 等,并根据这些指标确定覆盖视频的位置。由于模仿这种行为是一个复杂和棘手的过程,因此决定根据预定义的配置来控制覆盖视频的位置。

PiPManagerContentInsetAdjustmentNavigationBar

包括安全区域内边距和顶部安全区域额外的 44dp(横屏时为 32dp)的内边距。

PiPManagerContentInsetAdjustmentTabBar

包括安全区域内边距和底部安全区域额外的 49dp(iOS 11 及以上版本的横屏时为 32dp)的内边距。

PiPManagerContentInsetAdjustmentNavigationAndTabBars

包括 PiPManagerContentInsetAdjustmentNavigationBarPiPManagerContentInsetAdjustmentTabBar

PiPManagerContentInsetAdjustmentSafeArea

包括安全区域内边距。

PiPManagerContentInsetAdjustmentNone

覆盖视频被固定在屏幕边缘。

此行为可以通过 PiPManagercontentInsetAdjustmentBehavior 属性来更改。

Objective-C

PiPManager.contentInsetAdjustmentBehavior = PiPManagerContentInsetAdjustmentNavigationAndTabBars;

// animated
[UIView animateWithDuration:0.25 animations:^{
	PiPManager.contentInsetAdjustmentBehavior = PiPManagerContentInsetAdjustmentNavigationAndTabBars;
}];

Swift

PiPManager.contentInsetAdjustmentBehavior = .navigationAndTabBars

// animated
UIView.animate(withDuration: 0.25) {
	PiPManager.contentInsetAdjustmentBehavior = .navigationAndTabBars
}

额外的内容内边距

您还可以设置额外的屏幕边缘间距,这些间距将添加到自动计算的间距中。

Objective-C

PiPManager.additionalContentInsets = UIEdgeInsetsMake(20, 10, 20, 10);

// animated
[UIView animateWithDuration:0.25 animations:^{
	PiPManager.additionalContentInsets = UIEdgeInsetsMake(20, 10, 20, 10);
}];

Swift

PiPManager.additionalContentInsets.top = 20

// animated
UIView.animate(withDuration: 0.25) {
	PiPManager.additionalContentInsets.top = 20
}

禁用画中画

您可以通过禁用画中画模式临时禁用AVPictureInPictureControllerisPictureInPicturePossible属性为false。注意:这仅适用于iPhone。

Objective-C

// enable
PiPManager.pictureInPicturePossible = YES;

// disable
PiPManager.pictureInPicturePossible = NO;

Swift

// enable
PiPManager.isPictureInPicturePossible = true

// disable
PiPManager.isPictureInPicturePossible = false

待办事项

  • 减少私有API使用
  • 研究PiPPictureInPictureController是否应该成为AVPictureInPictureController的子类
  • 实现旋转手势,在捏合手势中添加“软”减速度
  • 改进自动内边距计算

示例

要运行示例项目,请首先从Example目录中克隆仓库,然后执行pod install

感谢 & 致谢

授权

PiPhone遵循MIT授权。有关更多信息,请参阅LICENSE文件。