我构建了一个应用程序,可以通过 Instagram API 从 Instagram 用户获取所有照片,并在 CollectionView 中按顺序显示它们。
现在,当用户点击照片时,照片将在 UIView
中打开以显示该图像 - 基本上用户可以捏合缩放以及其他 Instagram 应用中无法提供的选项.
我想做的是 - 滑动图像以移至下一张图像。 我考虑过使用 ScrollView,这没什么大不了的。
问题是我不知道如何调用收藏 View 中的下一张照片。
最佳答案
您可以使用类似这样的委托(delegate),让您的 View 显示图像,在需要时调用 nextPhoto() , Collection View 将提供。
struct Photo {
var image:UIImage?
}
class CustomCollectionView: UICollectionViewController{
var dataSource:[Photo]! = []
var selectedIndex:NSIndexPath!
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension CustomCollectionView: UpdatePhoto {
func nextPhoto() -> Photo?{
let nextIndex = selectedIndex.row + 1
// update selected index
selectedIndex = NSIndexPath(forRow: nextIndex, inSection: 0)
return dataSource[selectedIndex.row];
}
func previousPhoto() -> Photo? {
let previousIndex = selectedIndex.row - 1
// update selected index
selectedIndex = NSIndexPath(forRow: previousIndex , inSection: 0)
return dataSource[selectedIndex.row];
}
}
protocol UpdatePhoto {
func nextPhoto() -> Photo?
func previousPhoto() -> Photo?
}
class PhotoDisplayingView:UIView{
var photo:Photo
var delegate:UpdatePhoto?
required init(photo:Photo){
self.photo = photo
super.init(frame: CGRect(origin: CGPointZero, size: photo.image!.size))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
}
处理手势时你会做这样的事情
var nextImage = delegate.nextPhoto()
关于ios - 从其他类调用 CollectionView 中的下一张图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37598619/