AXPhotoViewer 1.7.1

AXPhotoViewer 1.7.1

测试已测试
Lang语言 SwiftSwift
许可 MIT
发布最后一个版本2019年4月
SPM支持 SPM

Alex Hill维护。



  • Alex Hill

AXPhotoViewer 构建状态

Demo GIF #1 Demo GIF #2

AXPhotoViewer 是一个适用于 iOS/tvOS 的照片查看器,非常适合查看大量(或非常少量!)的图片和 GIF。这个库支持上下文式显示和消失,交互式的“轻扫以消失”行为,并且可以轻松与许多第三方异步图片下载/缓存库集成。

如何使用

尽管 AXPhotoViewer 在其各个模块上提供了许多可配置属性,但抛下一些初始化代码并开始使用是很容易的

let dataSource = AXPhotosDataSource(photos: self.photos)
let photosViewController = PhotosViewController(dataSource: dataSource)
self.present(photosViewController, animated: true)

如何实现 force touch 功能?

可以通过使用 PreviewingPhotosViewController 轻松地使用这个库实现 force touch 功能

func previewingContext(_ previewingContext: UIViewControllerPreviewing,
                      viewControllerForLocation location: CGPoint) -> UIViewController? {

    guard let indexPath = self.tableView.indexPathForRow(at: location),
        let cell = self.tableView.cellForRow(at: indexPath),
        let imageView = cell.imageView else {
        return nil
    }

    previewingContext.sourceRect = self.tableView.convert(imageView.frame, from: imageView.superview)

    let dataSource = AXPhotosDataSource(photos: self.photos, initialPhotoIndex: indexPath.row)
    let previewingPhotosViewController = PreviewingPhotosViewController(dataSource: dataSource)

    return previewingPhotosViewController
}

func previewingContext(_ previewingContext: UIViewControllerPreviewing,
                      commit viewControllerToCommit: UIViewController) {

    if let previewingPhotosViewController = viewControllerToCommit as? PreviewingPhotosViewController {
        self.present(PhotosViewController(from: previewingPhotosViewController), animated: false)
    }
}

Objective-C互操作性

该库完全支持Objective-C和Swift代码库之间的互操作性。如果您在此遇到任何问题,请打开一个Github issues或提交一个包含建议更改的pull request。

安装

可以通过Cocoapods轻松完成安装。

pod install 'AXPhotoViewer'

如果您不想使用Cocoapods,可以将“Source”目录的内容添加到您的项目中以开始。

注意:如果您不使用Cocoapods,您必须将MobileCoreServices.frameworkQuartzCore.frameworkImageIO.framework添加到您的项目中。

配置

在显示您的AXPhotosViewController之前,有许多可配置的属性可以设置。

例如,在AXPhotoDataSource对象上,您可以设置初始页面索引作为数据源,还可以控制库下载更多照片的速率。

let photos = [first, second]
let dataSource = AXPhotosDataSource(photos: photos, initialPhotoIndex: 1, prefetchBehavior: .aggressive)

AXPagingConfig对象上,您可以设置不同的导航方向(页面间的水平或垂直滚动),照片间的间距(每个照片之间的点间距)和/或库在需要时将它实例化的自定义加载视图类。

let pagingConfig = AXPagingConfig(navigationOrientation: .horizontal, interPhotoSpacing: 20, loadingViewClass: CustomLoadingView.self)

最后但同样重要的是,是AXTransitionInfo配置。这可以用于自定义与您的AXPhotosViewController的显示和消失相关的所有事项,包括起始参考视图、结束参考视图、动画的持续时间以及禁用/启用交互式消失的标志。

let transitionInfo = AXTransitionInfo(interactiveDismissalEnabled: false, startingView: self.startingImageView) { [weak self] (photo, index) -> UIImageView? in
    // this closure can be used to adjust your UI before returning an `endingImageView`.
    return self?.endingImageView
}

自定义视图

将自定义自适应视图添加到相册查看器的视图层次结构中非常简单,只需访问 OverlayView 的属性即可。 topStackContainerbottomStackContainer 的名称恰如其分,它们固定在覆盖层的顶部或底部。要将自定义视图添加到这些容器中,只需以下步骤

photosViewController.overlayView.topStackContainer.addSubview(customSubview1)
photosViewController.overlayView.bottomStackContainer.addSubview(customSubview2)

这些视图将按照添加的顺序进行尺寸调整和堆叠。您可以根据需要重新排列这些子视图。

网络集成

在这个库中,网络集成是一个符合 AXNetworkIntegration 协议的类。此协议定义了一些用于下载图像以及将它们的完成(和错误)委托给库的方法。如果您想创建自己的异步下载/缓存图像和 GIF 的模块,则协议相当轻量级。

一些预定义的 AXNetworkIntegrations 已经作为 Cocoapod 子规范制作(如 SDWebImage、PINRemoteImage、AFNetworking、Kingfisher、Nuke...,以及一个简单的使用 NSURLSession 的网络集成,这足以满足大多数人的需求)。要使用这些预定义的子规范,只需更改您的 Podfile

pod install 'AXPhotoViewer/Lite'
pod install 'AXPhotoViewer/SDWebImage'
pod install 'AXPhotoViewer/PINRemoteImage'
pod install 'AXPhotoViewer/AFNetworking'
pod install 'AXPhotoViewer/Kingfisher'
pod install 'AXPhotoViewer/Nuke'

创建自己的 AXNetworkIntegration

pod install 'AXPhotoViewer/Core'
let customNetworkIntegration = CustomNetworkIntegration() // instantiate your custom network integration
let dataSource = AXPhotosDataSource(photos: self.photos)
let photosViewController = PhotosViewController(dataSource: dataSource, networkIntegration: customNetworkIntegration)

启用 Nuke 对 GIF 的支持

默认情况下,Nuke 对下载 GIF 的支持是禁用的。如果您想支持 GIF,则必须启用它。

ImagePipeline.Configuration.isAnimatedImageDataEnabled = true

更多详细信息请点击此处: Nuke 动画图像

自定义

如前所述,相册查看器的每个模块都有许多可配置的属性,您可以通过 AXPhotosViewController 访问这些模块。例如,您可以用自己的视图替换默认的加载视图、标题视图和/或叠加标题视图。这些视图必须能自动调整大小,并分别符合 AXLoadingViewProtocolAXCaptionViewProtocolAXOverlayTitleViewProtocol

let pagingConfig = AXPagingConfig(loadingViewClass: CustomLoadingView.self) // custom loading view class to be instantiated as necessary
...
photosViewController.overlayView.captionView = CustomCaptionView() // custom caption view
photosViewController.overlayView.titleView = CustomTitleView() // custom title view

AXPhotosViewController 及其模块非常易于扩展,因此可以轻松地通过子类模块来完成,而不会破坏库的其他区域。

贡献

如果您想看到一些你想修改的内容,我很高兴接受建议!如果在Github上看到错误,请创建一个issue,或者复制仓库并提交一个 pull request 来分享你的更改。我很乐意查看它们!