简体   繁体   English

从其他类调用collectionview中的下一个图像

[英]Calling next image in collectionview from other class

I built an app that fetch all the photos from Instagram user via Instagram API and show them ordered in CollectionView. 我构建了一个应用程序,可通过Instagram API从Instagram用户获取所有照片,并在CollectionView中显示它们的顺序。

Now, when the user tap on a photo the photo will open in a UIView to show that image - basically the user can pinch to zoom and other options that can't be in the Instagram app. 现在,当用户点击照片时,照片将在UIView打开以显示该图像-基本上,用户可以捏按缩放以及Instagram应用程序中无法提供的其他选项。

What I'm trying to do is - swipe the image to move to the next image. 我想做的是-滑动图像以移至下一张图像。 I thought about using ScrollView which is not a big deal. 我考虑过使用ScrollView没什么大不了的。

THE PROBLEM IS that I don't know how to call the next photo in the collection view. 问题是我不知道如何在收藏夹视图中调用下一张照片。

just store selected index path. 只需存储选定的索引路径。 Next photo will be next index path. 下一张照片将是下一个索引路径。

Example: self.selectedIndexPath = indexPath - save 示例: self.selectedIndexPath = indexPath保存

Then get next index path or nil (if not exist): 然后获取下一个索引路径或nil(如果不存在):

func nextIndexPath() -> NSIndexPath? {
  if collectionView.numberOfItemsInSection(selectedIndexPath.section) > selectedIndexPath.item {
      return NSIndexPath(forItem: selectedIndexPath.item + 1, inSection: selectedIndexPath.section)
  }
  return nil
}

Then you could use it to get next photo, something like this: 然后,您可以使用它来获取下一张照片,如下所示:

if let indexPath = nextIndexPath() {
  let photo = photos[indexPath.item]
}

PS item is the same as row , but for CollectionView, because row wrong name for it. PS itemrow相同,但对于CollectionView,因为它的row名称错误。 So it's preferable to use item in collection views 因此,最好在集合视图中使用item

You can use delegation something like this, have your view displaying the image call nextPhoto() when need an the collection view will supply. 您可以使用类似这样的委托,让您的视图在需要集合视图提供时显示图像调用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")
            }
        }
     }

You'll do something like this when handling the gesture var nextImage = delegate.nextPhoto() 处理手势时,您将执行以下操作var nextImage = delegate.nextPhoto()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM