示例
要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install
。
要求
安装
SwiftPic 通过 CocoaPods 提供。要安装它,只需在 Podfile 中添加以下行:
pod 'SwiftPic'
使用
展示 ImageViewController
let imageDetail = ImageViewController(configuration: configuration)
imageDetail.modalPresentationStyle = .custom
imageDetail.transitioningDelegate = self
present(imageDetail, animated: true, completion: nil)
配置
ImageViewController 类需要一个 ImageViewControllerConfiguration 的实例。
// The UIImage objects to display
let galleryImages = [image1, image2, ...]
// startIndex is 0 by default
let configuration = ImageViewControllerConfiguration(images: galleryImages, startIndex: 1)
// Optional closure called when image index changes, imeplement this to show/hide images
// when using the transition delegates
configuration.imageIndexChanged = { [weak self] index in
// Called each time the index updates
}
// Optional closure called when a new title is needed for an index change.
// Ignoring this will remove the title entirely
configuration.titleForImageAtIndex = { [weak self] index in
// return new title if required
}
过渡
SwiftPic 内部集成了过渡效果,在使用 UIViewControllerTransitioningDelegate 时,只需返回相关对象即可。无论是 ImageViewerAnimatorPresenting
还是 ImageViewerAnimatorDismissing
,都需要一个 UIImageview 实例来确定动画的起始和结束位置。请注意,当相册更改时,也应进行此更改。
extension MyUIViewController: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return ImageViewerAnimatorPresenting(originImageView: selectedImageView, animationTime: 0.5)
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return ImageViewerAnimatorDismissing(originImageView: selectedImageView, animationTime: 0.4)
}
}
UICollectionView 示例(来自示例项目)
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? PlanetCollectionViewCell {
selectedCell = cell
}
var configuration = ImageViewControllerConfiguration(images: images, startIndex: indexPath.row)
configuration.imageIndexChanged = { [weak self] index in
guard let weakSelf = self
else {return}
weakSelf.selectedCell?.imageView.isHidden = false
let updatedIndex = IndexPath(item: index, section: 0)
if let updatedCell = weakSelf.collectionView?.cellForItem(at: updatedIndex) as? PlanetCollectionViewCell {
weakSelf.selectedCell = updatedCell
updatedCell.imageView.isHidden = true
}
}
configuration.titleForImageAtIndex = { [weak self] index in
guard let weakSelf = self
else {return ""}
return weakSelf.planetImages[index].name
}
let imageDetail = ImageViewController(configuration: configuration)
imageDetail.modalPresentationStyle = .custom
imageDetail.transitioningDelegate = self
self.present(imageDetail, animated: true, completion: nil)
}
}
extension ImageCollectionViewController: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
if let cell = selectedCell {
return ImageViewerAnimatorPresenting(originImageView: cell.imageView, animationTime: 0.5)
}
return nil
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
if let cell = selectedCell {
return ImageViewerAnimatorDismissing(originImageView: cell.imageView, animationTime: 0.4)
}
return nil
}
}
作者
K2M
许可
SwiftPic 在 MIT 许可下提供。有关更多信息,请参阅 LICENSE 文件。