UICollectionViewFlowLayout.scrollingDirection更改强制重新加载-Swift 4

时间:2018-07-16 14:53:15

标签: swift uicollectionview rotation uicollectionviewlayout

我正在开发一个锁定为人像模式的应用程序。现在,我们在图片上添加了旋转手柄。

我有一个UICollectionView,每个单元格都采用全尺寸屏幕,以便全屏显示图片。事情是,您可以选择要查看的图片,然后在FullPictureViewController上显示全屏单元格。

当您更改手机的旋转角度时,会发生以下情况:

    func rotatePictures(cell: NewsImageCell) {
    DispatchQueue.main.async {
        switch UIDevice.current.orientation {
        case .landscapeLeft:
            let newImage : UIImage = UIImage(cgImage: cell.imageView.image!.cgImage!, scale: cell.imageView.image!.scale, orientation: UIImageOrientation.right)
            cell.imageView.image = newImage
            self.layout.scrollDirection = .vertical
            cell.layoutSubviews()
        case .landscapeRight:
            let newImage : UIImage = UIImage(cgImage: cell.imageView.image!.cgImage!, scale: cell.imageView.image!.scale, orientation: UIImageOrientation.left)
            cell.imageView.image = newImage
            self.layout.scrollDirection = .vertical
            cell.layoutSubviews()
        case .portrait:
            let newImage : UIImage = UIImage(cgImage: cell.imageView.image!.cgImage!, scale: cell.imageView.image!.scale, orientation: UIImageOrientation.up)
            cell.imageView.image = newImage
            self.layout.scrollDirection = .horizontal
            cell.layoutSubviews()
        case .portraitUpsideDown:
            let newImage : UIImage = UIImage(cgImage: cell.imageView.image!.cgImage!, scale: cell.imageView.image!.scale, orientation: UIImageOrientation.upMirrored)
            cell.imageView.image = newImage
            self.layout.scrollDirection = .horizontal
            cell.layoutSubviews()
        default:
            print("terminator")
        }
    }
}

现在,这是我的问题:如果删除self.layout.scrollDirection = .horizontal,我可以看到我的图片并旋转它,但是滚动方向将保持锁定在.vertical。但是,如果我切换self.layout.scrollDirection = .horizontal,collectionView将重新加载其数据源,然后将我发送回item [0]。

scrollDirection有关的任何事情似乎都将我带回到了第一项,而我想保留当前项。

有解决方案还是可以解决?

1 个答案:

答案 0 :(得分:1)

尝试安装此Pod:

https://github.com/Krisiacik/ImageViewer

并在调用此库方法时传递imageview对象。