从其他 class 调用 collectionview 中的下一张图片
Calling next image in collectionview from other class
我构建了一个应用程序,可以通过 Instagram API 从 Instagram 用户那里获取所有照片,并在 CollectionView 中按顺序显示它们。
现在,当用户点击一张照片时,照片将在 UIView
中打开以显示该图像 - 基本上,用户可以通过双指缩放和其他 Instagram 应用中无法提供的选项。
我想做的是 - 滑动图像以移动到下一张图像。
我考虑过使用 ScrollView,这没什么大不了的。
问题是我不知道如何在 collection 视图中调用下一张照片。
只存储选定的索引路径。下一张照片将是下一个索引路径。
示例:
self.selectedIndexPath = indexPath
- 保存
然后获取下一个索引路径或nil(如果不存在):
func nextIndexPath() -> NSIndexPath? {
if collectionView.numberOfItemsInSection(selectedIndexPath.section) > selectedIndexPath.item {
return NSIndexPath(forItem: selectedIndexPath.item + 1, inSection: selectedIndexPath.section)
}
return nil
}
然后你可以用它来获取下一张照片,像这样:
if let indexPath = nextIndexPath() {
let photo = photos[indexPath.item]
}
P.S。 item
与row
相同,但对于CollectionView,因为row
名称错误。所以最好在collection views
中使用item
您可以像这样使用委托,让显示图像的视图在需要时调用 nextPhoto() 并提供集合视图。
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()
我构建了一个应用程序,可以通过 Instagram API 从 Instagram 用户那里获取所有照片,并在 CollectionView 中按顺序显示它们。
现在,当用户点击一张照片时,照片将在 UIView
中打开以显示该图像 - 基本上,用户可以通过双指缩放和其他 Instagram 应用中无法提供的选项。
我想做的是 - 滑动图像以移动到下一张图像。 我考虑过使用 ScrollView,这没什么大不了的。
问题是我不知道如何在 collection 视图中调用下一张照片。
只存储选定的索引路径。下一张照片将是下一个索引路径。
示例:
self.selectedIndexPath = indexPath
- 保存
然后获取下一个索引路径或nil(如果不存在):
func nextIndexPath() -> NSIndexPath? {
if collectionView.numberOfItemsInSection(selectedIndexPath.section) > selectedIndexPath.item {
return NSIndexPath(forItem: selectedIndexPath.item + 1, inSection: selectedIndexPath.section)
}
return nil
}
然后你可以用它来获取下一张照片,像这样:
if let indexPath = nextIndexPath() {
let photo = photos[indexPath.item]
}
P.S。 item
与row
相同,但对于CollectionView,因为row
名称错误。所以最好在collection views
item
您可以像这样使用委托,让显示图像的视图在需要时调用 nextPhoto() 并提供集合视图。
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()