HXPhotoPicker中PhotoBrowser自定义与屏幕旋转实践
2025-06-25 18:34:13作者:魏侃纯Zoe
前言
HXPhotoPicker是一款功能强大的iOS照片选择器组件,其中PhotoBrowser模块提供了图片浏览功能。在实际开发中,我们经常需要对PhotoBrowser进行深度定制以满足业务需求。本文将详细介绍如何自定义PhotoBrowser的页面指示器、标题视图以及处理屏幕旋转逻辑。
自定义页面指示器
PhotoBrowser默认提供了页面指示器,但开发者可以通过实现PhotoBrowserPageIndicator协议来自定义样式。以下是实现自定义页面指示器的关键步骤:
-
创建自定义指示器类: 继承UIView并实现
PhotoBrowserPageIndicator协议,该协议要求实现以下方法:reloadData(numberOfPages: Int, pageIndex: Int)- 加载数据时调用didChanged(pageIndex: Int)- 页面切换时调用
-
示例实现:
class CustomPageIndicator: UIView, PhotoBrowserPageIndicator { private let pageLabel = UILabel() override init(frame: CGRect) { super.init(frame: frame) setupUI() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setupUI() { pageLabel.textColor = .white pageLabel.textAlignment = .center addSubview(pageLabel) } func reloadData(numberOfPages: Int, pageIndex: Int) { updateText(currentPage: pageIndex, totalPages: numberOfPages) } func didChanged(pageIndex: Int) { updateText(currentPage: pageIndex) } private func updateText(currentPage: Int, totalPages: Int? = nil) { if let total = totalPages { pageLabel.text = "\(currentPage + 1)/\(total)" } else { let components = pageLabel.text?.components(separatedBy: "/") if let first = components?.first { pageLabel.text = "\(currentPage + 1)/\(first)" } } } override func layoutSubviews() { super.layoutSubviews() pageLabel.frame = bounds } } -
设置自定义指示器:
let browser = PhotoBrowser(config: config, pageIndex: 0, assets: assets) browser.pageIndicator = CustomPageIndicator(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
自定义标题视图
PhotoBrowser的导航栏标题视图也可以自定义:
-
创建自定义标题视图:
class CustomTitleView: UIView { let titleLabel = UILabel() override init(frame: CGRect) { super.init(frame: frame) titleLabel.textColor = .white titleLabel.textAlignment = .center addSubview(titleLabel) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func layoutSubviews() { super.layoutSubviews() titleLabel.frame = bounds } } -
设置标题视图:
let titleView = CustomTitleView(frame: CGRect(x: 0, y: 0, width: 200, height: 30)) browser.previewViewController?.navigationItem.titleView = titleView
处理屏幕旋转
在图片浏览器中实现屏幕旋转功能需要注意以下几点:
-
配置旋转支持:
browser.config.shouldAutorotate = true browser.config.supportedInterfaceOrientations = .all -
实现旋转控制:
enum DeviceOrientationHelper { static func setInterfaceOrientation(_ orientation: UIInterfaceOrientation) { if #available(iOS 16.0, *) { guard let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene else { return } let orientationMask: UIInterfaceOrientationMask switch orientation { case .portrait: orientationMask = .portrait case .landscapeLeft: orientationMask = .landscapeLeft case .landscapeRight: orientationMask = .landscapeRight case .portraitUpsideDown: orientationMask = .portraitUpsideDown default: orientationMask = .allButUpsideDown } let geometryPreferences = UIWindowScene.GeometryPreferences.iOS(interfaceOrientations: orientationMask) scene.requestGeometryUpdate(geometryPreferences) } else { UIDevice.current.setValue(orientation.rawValue, forKey: "orientation") UIViewController.attemptRotationToDeviceOrientation() } } static var isLandscape: Bool { UIApplication.shared.windows.first?.windowScene?.interfaceOrientation.isLandscape ?? false } } -
在自定义指示器中添加旋转按钮:
let rotateButton = UIButton() rotateButton.setTitle("旋转", for: .normal) rotateButton.addTarget(self, action: #selector(rotateScreen), for: .touchUpInside) @objc private func rotateScreen() { if DeviceOrientationHelper.isLandscape { DeviceOrientationHelper.setInterfaceOrientation(.portrait) } else { DeviceOrientationHelper.setInterfaceOrientation(.landscapeRight) } }
自定义返回按钮行为
如果需要自定义左上角返回按钮的行为(例如在横屏时先返回竖屏),可以通过以下方式实现:
-
替换返回按钮事件:
browser.show(nil) DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { let item = browser.previewViewController?.navigationItem.leftBarButtonItem item?.action = #selector(self.customBackAction) item?.target = self } -
实现自定义返回逻辑:
@objc private func customBackAction() { if DeviceOrientationHelper.isLandscape { DeviceOrientationHelper.setInterfaceOrientation(.portrait) } else { browser.previewViewController?.dismiss(animated: true) } }
总结
通过对HXPhotoPicker中PhotoBrowser模块的自定义,我们可以实现更加符合业务需求的图片浏览器功能。关键点包括:
- 通过实现
PhotoBrowserPageIndicator协议自定义页面指示器 - 直接替换导航栏的titleView来自定义标题显示
- 使用系统API处理屏幕旋转逻辑
- 通过替换返回按钮事件实现自定义返回逻辑
这些定制方法不仅适用于HXPhotoPicker,其思路也可以应用于其他类似的图片浏览器组件中。开发者可以根据实际需求灵活组合这些技术点,打造出功能丰富、用户体验良好的图片浏览功能。
登录后查看全文
热门项目推荐
相关项目推荐
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00
GLM-4.7-FlashGLM-4.7-Flash 是一款 30B-A3B MoE 模型。作为 30B 级别中的佼佼者,GLM-4.7-Flash 为追求性能与效率平衡的轻量化部署提供了全新选择。Jinja00
VLOOKVLOOK™ 是优雅好用的 Typora/Markdown 主题包和增强插件。 VLOOK™ is an elegant and practical THEME PACKAGE × ENHANCEMENT PLUGIN for Typora/Markdown.Less00
PaddleOCR-VL-1.5PaddleOCR-VL-1.5 是 PaddleOCR-VL 的新一代进阶模型,在 OmniDocBench v1.5 上实现了 94.5% 的全新 state-of-the-art 准确率。 为了严格评估模型在真实物理畸变下的鲁棒性——包括扫描伪影、倾斜、扭曲、屏幕拍摄和光照变化——我们提出了 Real5-OmniDocBench 基准测试集。实验结果表明,该增强模型在新构建的基准测试集上达到了 SOTA 性能。此外,我们通过整合印章识别和文本检测识别(text spotting)任务扩展了模型的能力,同时保持 0.9B 的超紧凑 VLM 规模,具备高效率特性。Python00
KuiklyUI基于KMP技术的高性能、全平台开发框架,具备统一代码库、极致易用性和动态灵活性。 Provide a high-performance, full-platform development framework with unified codebase, ultimate ease of use, and dynamic flexibility. 注意:本仓库为Github仓库镜像,PR或Issue请移步至Github发起,感谢支持!Kotlin07
compass-metrics-modelMetrics model project for the OSS CompassPython00
最新内容推荐
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
521
3.71 K
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
12
1
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
67
20
暂无简介
Dart
762
183
喝着茶写代码!最易用的自托管一站式代码托管平台,包含Git托管,代码审查,团队协作,软件包和CI/CD。
Go
23
0
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.32 K
740
无需学习 Kubernetes 的容器平台,在 Kubernetes 上构建、部署、组装和管理应用,无需 K8s 专业知识,全流程图形化管理
Go
16
1
React Native鸿蒙化仓库
JavaScript
302
348
基于golang开发的网关。具有各种插件,可以自行扩展,即插即用。此外,它可以快速帮助企业管理API服务,提高API服务的稳定性和安全性。
Go
22
1