在 CollectionViewcell 中实现 UIScrollView

问题描述

我正在使用一个 collectionViewCell,其中我有一个 scrollView>view>imageview,这意味着一个视图嵌入在 scrollView 中,一个 imageView 嵌入在视图中。我已将 CollectionViewCell 类设置为 scrollView Delegate 并使用函数 viewForZooming,因此当用户点击它时,它将以最大比例缩放,但没有任何反应。以下是我的 CollectionViewCell代码

class CollectionViewCell: UICollectionViewCell,uiscrollviewdelegate {
    
    var imageView: UIImageView = {
        var myImageView = UIImageView()
        myImageView.contentMode = .scaleAspectFill
        return myImageView
    }()
    
    var scrollView: UIScrollView = UIScrollView()
    var scrollContentView: UIView = UIView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(scrollView)
        scrollView.addSubview(scrollContentView)
        scrollContentView.addSubview(imageView)
        scrollView.isScrollEnabled = true
        
        scrollView.delegate = self
        scrollView.maximumZoomScale = 5.0
        
        let constraints = [
            // Scroll View Constraints
            scrollView.topAnchor.constraint(equalTo: self.contentView.topAnchor,constant: 0.0),scrollView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor,scrollView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor,scrollView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor,// scrollContentView Constraints
            scrollContentView.topAnchor.constraint(equalTo: self.scrollView.topAnchor,scrollContentView.bottomAnchor.constraint(equalTo: self.scrollView.bottomAnchor,scrollContentView.trailingAnchor.constraint(equalTo: self.scrollView.trailingAnchor,scrollContentView.leadingAnchor.constraint(equalTo: self.scrollView.leadingAnchor,scrollContentView.heightAnchor.constraint(equalTo: self.scrollView.heightAnchor,scrollContentView.widthAnchor.constraint(equalTo: self.scrollView.widthAnchor,// ImageView Constraints
            imageView.topAnchor.constraint(equalTo: self.scrollContentView.topAnchor,imageView.bottomAnchor.constraint(equalTo: self.scrollContentView.bottomAnchor,imageView.trailingAnchor.constraint(equalTo: self.scrollContentView.trailingAnchor,imageView.leadingAnchor.constraint(equalTo: self.scrollContentView.leadingAnchor,constant: 0.0)
        ]
        
        NSLayoutConstraint.activate(constraints)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
    }
}

我在 CollectionViewClass 中使用的代码

func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell",for: indexPath) as! CollectionViewCell
    
    cell.imageView.image =  selectedImage[indexPath.item]
    
    cell.scrollView.frame = CGRect(x: 0,y: 0,width: cell.imageView.image!.size.width,height: cell.imageView.image!.size.height)
    cell.scrollContentView.frame = CGRect(x: 0,width: cell.scrollView.frame.width,height: cell.scrollView.frame.height)
    cell.imageView.frame = CGRect(x: 0,width: cell.frame.width,height: cell.frame.height)
    
    cell.scrollView.clipsToBounds = true
    cell.scrollContentView.clipsToBounds = true
    cell.imageView.clipsToBounds = true
    
    cell.backgroundColor = .black
    
    return cell
}

我看到一个 YouTube 教程的人也在做同样的事情,只是它不是针对 collectionViewCell。 我认为我的 scrollView 甚至没有响应触摸。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)