繁体   English   中英

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

[英]Calling next image in collectionview from other class

我构建了一个应用程序,可通过Instagram API从Instagram用户获取所有照片,并在CollectionView中显示它们的顺序。

现在,当用户点击照片时,照片将在UIView打开以显示该图像-基本上,用户可以捏按缩放以及Instagram应用程序中无法提供的其他选项。

我想做的是-滑动图像以移至下一张图像。 我考虑过使用ScrollView没什么大不了的。

问题是我不知道如何在收藏夹视图中调用下一张照片。

只需存储选定的索引路径。 下一张照片将是下一个索引路径。

示例: 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]
}

PS itemrow相同,但对于CollectionView,因为它的row名称错误。 因此,最好在集合视图中使用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()

暂无
暂无

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

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