ios - 从其他类调用 CollectionView 中的下一张图像

标签 ios swift uicollectionview uicollectionviewcell instagram-api

我构建了一个应用程序,可以通过 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/

相关文章:

iphone - 如果 self.bitmask |= 标志添加了一个选项,如何删除一个?

Swift:使用字符串作为名称在运行时创建对象

ios - Swift:从 SwiftyJson 结果创建结果表

xcode - AVFoundation 框架中不存在 AVAudioSession

ios - 在第一个单元格加载到 UICollectionView 之前获取 UICollectionViewCell

ios - UIView subview 向前触摸

ios - 如何在 iOS 中覆盖 Cordova 的 audioCapture 接口(interface)?

ios UICollectionViewCell scroller时间自动重新加载如何停止

ios - 如果用户正在滚动,则在 UICollectionView 中禁用自动滚动

ios UICollectionView 以编程方式删除标题