这更多的是一个方法问题。假设我有一个 Collection View ,其中每个单元格占据整个屏幕,其中包含图像和一些其他数据(例如:标题、信息等)。我想要的是用户点击单元格和图像即可进入全屏模式。我可以通过使用此代码初始化单独的 View 并缩放图像以适合屏幕来实现此目的。
let newImageView = UIImageView(image: imageView.image)
newImageView.frame = UIScreen.main.bounds
newImageView.backgroundColor = .black
newImageView.contentMode = .scaleAspectFit
然后通过删除 View 来关闭它,如下所示:
self.navigationController?.isNavigationBarHidden = false
navigationController?.isToolbarHidden = false
sender.view?.removeFromSuperview()
}
Q1。有没有一种方法可以操纵所有 Collection View 单元格,使其在点击时转换为全屏 View ,以便我可以使用 Collection View 的默认滑动操作来水平滚动图像?
第二季度。如果没有,我使用这个库 INSPhotoGallery !添加点击效果,这给了我想要的效果,但由于从 PHAsset 库加载大量图像,我的应用程序崩溃了。
这就是我初始化阶段集以传递到该库的方式:
lazy var photos: [INSPhotoViewable] = {
var allPhotos: [INSPhoto] = Array()
fetchResult.enumerateObjects({ (asset, index, stop) in
let image = self.requestImageForPHAsset(asset: asset)
allPhotos.append(INSPhoto(image: image, thumbnailImage: nil))
})
return allPhotos
}()
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
print("You selected cell #\(indexPath.item)!")
let cell = collectionView.cellForItem(at: indexPath) as! DetailedCollectionViewCell
let currentPhoto = photos[indexPath.row]
let galleryPreview = INSPhotosViewController(photos: photos, initialPhoto: currentPhoto, referenceView: cell)
galleryPreview.overlayView.photosViewController?.singleTapGestureRecognizer.isEnabled = false
galleryPreview.referenceViewForPhotoWhenDismissingHandler = { [weak self] photo in
if let index = self?.photos.index(where: {$0 === photo}) {
let indexPath = IndexPath(item: index, section: 0)
return collectionView.cellForItem(at: indexPath) as? DetailedCollectionViewCell
}
return nil
}
present(galleryPreview, animated: true, completion: nil)
}
问题是如何防止我的应用程序崩溃。我知道这与缓存图像和异步加载有关。如果没有的话,您是否知道与 PHAssets 集成良好的库?谢谢!
最佳答案
- 您还可以使用页面 View Controller 来实现相同的功能。如果您的细胞少于 5 个,它就很有值(value)。
- 您可以全屏模式显示图像。只需创建单独的 View Controller 并为此模式屏幕实现动画师(转换委托(delegate))即可。这将是更困难但也是更正确的方式。 AppStore 以同样的方式工作
关于ios - Collection View 的全屏 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54581794/