Jane Photo Browser
概览
JanePhotoBrowser 是一个用 Swift 3 编写的滚动照片画廊。与照片浏览器交互的 API 与在 UITableViewDataSource 和 UICollectionViewDelegate 中使用的 API 非常相似,应该感觉熟悉。
特性
- 简单设置:只需将 PhotoBrowserView 添加到您的 ViewController 中,并实现 datasource 和 delegate
- 全屏浏览器:在 PhotoView 中点击图片以启动全屏浏览器。
- 滑动手势:通过点击关闭按钮、点击图片或向上滑动来关闭全屏浏览器。
- Swift:此项目完全用 Swift 编写。
Swift 版本
要使用 3.0
版本的 Swift,请使用标签 0.2.*
要使用 2.3
版本的 Swift,请使用标签 0.1.*
设置
要开始使用 JanePhotoBrowser,您可以使用 Cocoapods 安装,或者将 Class
文件夹中的文件添加到您的项目中。
您可以在Storyboard中添加 PhotoBrowserView,或者通过调用一个初始化器以编程方式添加,并设置数据源和代理。
@IBOutlet weak var photoView:PhotoBrowserView?
override func viewDidLoad() {
super.viewDidLoad
self.photoView.dataSource = self
self.photoView.delegate = self
}
如果您的代理是 UIViewController,则 PhotoBrowserDelegate 协议扩展中为您完成了默认的代理方法实现。
数据源
数据源协议提供了 PhotoBrowser 获取所需图像的方法。
数据源协议
public protocol PhotoBrowserDataSource:class {
func photoBrowser(photoBrowser:PhotoBrowserView, photoAtIndex index: Int, forCell cell:PhotoBrowserViewCell) -> UIImage
func numberOfPhotos(photoBrowser:PhotoBrowserView) -> Int
}
通过 numberOfPhotos
方法让 PhotoBrowser 知道需要多少照片。这类似于 UITableViewDataSource
和 UICollectionViewDataSource
的方法。
photoBrowser(photoBrowser:PhotoBrowserView, photoAtIndex index: Int, forCell cell:PhotoBrowserViewCell) -> UIImage
为指定索引返回一个 UIImage
。如果您已经有了一个图像列表,您只需在这个方法中返回它们。如果您必须从线上资源拉取图像,您可以做如下操作:
func photoBrowser(photoBrowser: PhotoBrowserView, photoAtIndex index: Int, forCell cell:PhotoBrowserViewCell) -> UIImage {
guard let URL = self.deal?.allImageUrls()[index] else { return UIImage() }
var returnImage: UIImage = self.loadingImage
//Get image from cache or online if not cached.
//Note that Image caching is not included in the project.
MyImageCacheClass.image(atURL: URL) { (image) in
//If the image was fetched online, this method may have already
//returned the loading image, so set the image on the
//photoBrowserViewCell as well as setting the returnImage.
cell.imageView.image = image
returnImage = image
}
return returnImage
}
许可证
本项目采用 MIT 许可证发布。
反馈
如果您在此项目中遇到任何问题或功能请求,请创建问题并/或向我们发送拉取请求。
希望您喜欢JanePhotoBrowser!