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,其思路也可以应用于其他类似的图片浏览器组件中。开发者可以根据实际需求灵活组合这些技术点,打造出功能丰富、用户体验良好的图片浏览功能。
登录后查看全文
热门项目推荐
相关项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5.1GLM-5.1是智谱迄今最智能的旗舰模型,也是目前全球最强的开源模型。GLM-5.1大大提高了代码能力,在完成长程任务方面提升尤为显著。和此前分钟级交互的模型不同,它能够在一次任务中独立、持续工作超过8小时,期间自主规划、执行、自我进化,最终交付完整的工程级成果。Jinja00
MiniMax-M2.7MiniMax-M2.7 是我们首个深度参与自身进化过程的模型。M2.7 具备构建复杂智能体应用框架的能力,能够借助智能体团队、复杂技能以及动态工具搜索,完成高度精细的生产力任务。Python00- QQwen3.5-397B-A17BQwen3.5 实现了重大飞跃,整合了多模态学习、架构效率、强化学习规模以及全球可访问性等方面的突破性进展,旨在为开发者和企业赋予前所未有的能力与效率。Jinja00
HY-Embodied-0.5这是一套专为现实世界具身智能打造的基础模型。该系列模型采用创新的混合Transformer(Mixture-of-Transformers, MoT) 架构,通过潜在令牌实现模态特异性计算,显著提升了细粒度感知能力。Jinja00
LongCat-AudioDiT-1BLongCat-AudioDiT 是一款基于扩散模型的文本转语音(TTS)模型,代表了当前该领域的最高水平(SOTA),它直接在波形潜空间中进行操作。00
项目优选
收起
deepin linux kernel
C
28
15
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
663
4.27 K
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.54 K
895
Ascend Extension for PyTorch
Python
505
610
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
392
290
暂无简介
Dart
909
219
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
69
21
昇腾LLM分布式训练框架
Python
142
168
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
940
867
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
1.33 K
108