Swift 的 PhotoSlider
PhotoSlider 是一个简单的照片滑块,可以通过滑动来删除滑块。
要求
- Xcode 9+
- Swift 4.0+
- iOS 10+
安装
CocoaPods
PhotoSlider 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod "PhotoSlider"
Carthage
Carthage 是 Cocoa 应用程序的分布式依赖项管理器。
$ brew update
$ brew install carthage
要将 PhotoSlider 集成到您的 Xcode 项目中,并使用 Carthage,请在 Cartfile
中指定它。
github "nakajijapan/PhotoSlider"
然后,运行以下命令构建 PhotoSlider 框架:
$ carthage update
用途
使用 ZoomingAnimationControllerTransitioning
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
var slider = PhotoSlider.ViewController(imageURLs: self.images)
slider.currentPage = indexPath.row
photoSlider.transitioningDelegate = self
present(photoSlider, animated: true, completion: nil)
}
ZoomingAnimationControllerTransitioning
返回起始位置的 imageView
// MARK: ZoomingAnimationControllerTransitioning
func transitionSourceImageView() -> UIImageView {
let indexPath = collectionView.indexPathsForSelectedItems?.first
let cell = collectionView.cellForItem(at: indexPath!) as! ImageCollectionViewCell
let imageView = UIImageView(image: cell.imageView.image)
var frame = cell.imageView.frame
frame.origin.y += UIApplication.shared.statusBarFrame.height
imageView.frame = frame
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFill
return imageView
}
返回完成位置的 sourceImageView
func transitionDestinationImageView(sourceImageView: UIImageView) {
guard let image = sourceImageView.image else {
return
}
let indexPath = collectionView.indexPathsForSelectedItems?.first
let cell = collectionView.cellForItem(at: indexPath!) as! ImageCollectionViewCell
let statusBarHeight = UIApplication.shared.statusBarFrame.height
// snip..
sourceImageView.frame = frame
}
UIViewControllerTransitioningDelegate
// MARK: UIViewControllerTransitioningDelegate
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
let animationController = PhotoSlider.ZoomingAnimationController(present: false)
animationController.sourceTransition = dismissed as? ZoomingAnimationControllerTransitioning
animationController.destinationTransition = self
return animationController
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
let animationController = PhotoSlider.ZoomingAnimationController(present: true)
animationController.sourceTransition = source as? ZoomingAnimationControllerTransitioning
animationController.destinationTransition = presented as? ZoomingAnimationControllerTransitioning
return animationController
}
使用 UIModalTransitionStyle
选择 ZoomingAnimationController
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
var slider = PhotoSlider.ViewController(imageURLs: self.images)
slider.modalPresentationStyle = .OverCurrentContext
slider.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
slider.index = indexPath.row
self.presentViewController(slider, animated: true, completion: nil)
}
代理
您可以用以下事件处理:
- 可选 func photoSliderControllerWillDismiss(viewController: PhotoSlider.ViewController)
- 可选 func photoSliderControllerDidDismiss(viewController: PhotoSlider.ViewController)
多图加载器
PhotoSlider使用Kingfisher进行远程图片加载。如果您的项目使用SDWebImage,Kingfisher和SDWebImage之间不会共享图片缓存。在这种情况下,您可以使用自定义ImageLoader。默认的ImageLoader是Kingfisher。
以下是更改SDWebImage的方法。
首先,创建一个自定义的ImageLoader。
import PhotoSlider
class PhotoSliderSDImageLoader: PhotoSlider.ImageLoader {
public func load(
imageView: UIImageView?,
fromURL url: URL?,
progress: @escaping PhotoSlider.ImageLoader.ProgressBlock,
completion: @escaping PhotoSlider.ImageLoader.CompletionBlock)
{
// Webp compatibility (optional)
let WebPCoder = SDImageWebPCoder.shared
SDImageCodersManager.shared.addCoder(WebPCoder)
imageView?.sd_setImage(
withURL: url,
placeholderImage: nil,
options: SDWebImageOptions.retryFailed,
progress: { (receivedSize, totalSize) in
progress(receivedSize, totalSize)
},
completed: { (image, _, _, _) in
completion(image)
}
)
}
}
然后,设置ImageLoader。
let slider = PhotoSlider.ViewController(imageURLs: images)
slider.modalPresentationStyle = .OverCurrentContext
slider.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
slider.index = indexPath.row
slider.imageLoader = PhotoSliderSDImageLoader()
present(slider, animated: true, completion: nil)
作者
nakajijapan
鸣谢
- hikarock
- yhkaplan
- seapy
- antrix1989
贡献者
支持者
感谢所有的支持者!
赞助商
成为这个项目的赞助者。您的标志将在这里显示,并链接到您的网站。[成为赞助者]
许可证
PhotoSlider 采用 MIT 许可证。更多信息请参阅 LICENSE 文件。