Swift:按单元格图像大小滚动滚动视图

时间:2018-12-21 12:26:15

标签: ios swift uiscrollview uicollectionview

我有一个viewController,其中有CollectionView,collectionView单元格包含ImageView,响应来自apiviewController orientationLandscape。所以问题是,当我滚动图像时,滚动不正确。这是附件图像。

image 1 image 2

那么如何根据我的图像尺寸设置Scrolling action。因此,它仅在View中显示图像。这是我的代码。

extension GalleryViewController : UICollectionViewDataSource , UICollectionViewDelegateFlowLayout {

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell
            let image = arrImgs[indexPath.row]
            cell.lblIndex.text = "< \(indexPath.row+1) / \(arrImgs.count) >  Images"
            if let imgUrl = URL.init(string: image.Message) {
                cell.imgV.sd_setImage(with: imgUrl) { (img, error, casheType, url) in
                    DispatchQueue.main.async {
                        cell.imgV.image = img
                    }
                }
            }
            return cell
        }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return arrImgs.count
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            return CGSize.init(width: width, height: height)
    }
}

class ImageCell: UICollectionViewCell {

    @IBOutlet var lblIndex: UILabel!
    @IBOutlet var imgV: UIImageView!

}

这是我的StoryBord安排。

image 3

谢谢。

0 个答案:

没有答案