程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用 Swift 在 iOS 上的 UICollectionView 中设置两张照片之间的间隔?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何使用 Swift 在 iOS 上的 UICollectionView 中设置两张照片之间的间隔??

开发过程中遇到如何使用 Swift 在 iOS 上的 UICollectionView 中设置两张照片之间的间隔?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何使用 Swift 在 iOS 上的 UICollectionView 中设置两张照片之间的间隔?的解决方法建议,希望对你解决如何使用 Swift 在 iOS 上的 UICollectionView 中设置两张照片之间的间隔?有所启发或帮助;

我想实现照片占满整个屏幕的效果,在相册中滚动照片时,两张照片之间会有一个间隔,滑动后照片会自动弹回中心。

我设置了 @H_896_5@minimumlinespacing 和 isPagingEnabled,但是当我滑动时,图像没有填满整个屏幕,而是左侧显示了间距。

有人知道如何实现吗?

代码:

layout.itemSize = CGSize(wIDth: UIScreen.main.bounds.size.wIDth,height: UIScreen.main.bounds.size.height)
layout.scrollDirection = .horizontal
layout.minimumlinespacing = 10
vIEw.addSubvIEw(mCollectionVIEw)
mCollectionVIEw.isPagingEnabled = true
mCollectionVIEw.BACkgroundcolor = .white
mCollectionVIEw.register(UINib(nibname: "PhotoDetailCVCell",bundle: nil),forCellWithReusEIDentifIEr: cvCellIDentifIEr)
mCollectionVIEw.snp.makeConsTraints { (makE) in
    make.edges.equalTo(vIEw)
}

目标效果:

休息时:

如何使用 Swift 在 iOS 上的 UICollectionView 中设置两张照片之间的间隔?

当我们移动它时:

如何使用 Swift 在 iOS 上的 UICollectionView 中设置两张照片之间的间隔?

而目前的风格仍然是:

如何使用 Swift 在 iOS 上的 UICollectionView 中设置两张照片之间的间隔?

解决方法

这是一种方法 - 它可能适合也可能不适合您的需求...

对于你的单元格布局,给图像视图每边 5 点“填充”:

如何使用 Swift 在 iOS 上的 UICollectionView 中设置两张照片之间的间隔?

当单元格相邻时(流布局设置为 .minimumLinespacing = 0.minimumInteritemSpacing = 0),视觉效果将使它看起来之间有 10 pts 的间距图片:

如何使用 Swift 在 iOS 上的 UICollectionView 中设置两张照片之间的间隔?

接下来,对集合视图进行布局,使其比控制器的视图

如何使用 Swift 在 iOS 上的 UICollectionView 中设置两张照片之间的间隔?

当您拖动时,单元格之间会出现 10 pts 的间距:

如何使用 Swift 在 iOS 上的 UICollectionView 中设置两张照片之间的间隔?

.isPagingEnabled = true 会将单元格“对齐”到位:

如何使用 Swift 在 iOS 上的 UICollectionView 中设置两张照片之间的间隔?

这是一个完整的示例 - 注意:这是示例代码!!

带有 UIImageView 的基本单元类:

class PhotoDetailCVCell: UICollectionViewCell {
    
    let imgView: UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFit
        return v
    }()
    
    override init(frame: CGRect) {
        super.init(frame: framE)
        commonInit()
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }
    func commonInit() -> Void {
        
        contentView.addSubview(imgView)

        // consTrain image view
        imgView.snp.makeConsTraints { (makE) in
            //  top/bottom
            make.top.bottom.equalTo(contentView)
            //  leading/Trailing inset by 5-pts
            make.leading.Trailing.equalTo(contentView).inset(5.0)
        }

    }
    
}

示例控制器类:

class ViewController: UIViewController,UICollectionViewDatasource,UICollectionViewDelegate {
    
    let cvCellIdentifier: String = "photoCVCell"
    
    var mCollectionView: UICollectionView!
    
    var imgNames: [String] = [
        "p1","p2","p3","p4","p5",]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let layout = UICollectionViewFlowLayout()
        
        layout.scrollDirection = .horizontal
        layout.minimumLinespacing = 0
        layout.minimumInteritemSpacing = 0
        
        mCollectionView = UICollectionView(frame: .zero,collectionViewLayout: layout)
        mCollectionView.isPagingEnabled = true
        mCollectionView.BACkgroundColor = .white

        view.addSubview(mCollectionView)
        
        // respect safe area
        mCollectionView.snp.makeConsTraints { (makE) in
            // top/bottom equal to safe area
            make.top.bottom.equalTo(view.safeAreaLayoutGuidE)
            // leading/Trailing equal to safe area inset by -5.0
            //  this makes the collection view 10-pts WIDER than the view
            //  so having 5-pts "spacing" on each side of the cell's imageView
            //  will give the appearance of 10-pts spacing between cells
            make.leading.Trailing.equalTo(view.safeAreaLayoutGuidE).inset(-5.0)
        }

        // if you're using a XIB for your cell
        //mCollectionView.register(UINib(nibName: "PhotoDetailCVCell",bundle: nil),forCellWithReusEIDentifier: cvCellIdentifier)
        
        // if you're using code (as in this examplE) for your cell
        mCollectionView.register(PhotoDetailCVCell.self,forCellWithReusEIDentifier: cvCellIdentifier)
        
        mCollectionView.datasource = self
        mCollectionView.delegate = self
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        // set collection view itemSize here,since we now know
        //  the frame size of the collection view
        if let layout = mCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.itemSize = mCollectionView.frame.size
        }
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: int) -> Int {
        return imgNames.count
    }
    
    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReusEIDentifier: cvCellIdentifier,for: indexPath) as! PhotoDetailCVCell
        
        // try to load the image from Assets
        if let img = UIImage(named: imgNames[indexPath.item]) {
            cell.imgView.image = img
        } else {
            // could not  load image from Assets,so create a systemname image
            if let img = UIImage(systemname: "\(indexPath.item).square.fill") {
                cell.imgView.image = img
            }
        }
        
        // set image view BACkground color if you want to see its frame
        //cell.imgView.BACkgroundColor = .lightGray

        return cell
    }
    
}
,

这让我困惑了好几天,最后我找到了解决方案。解决方法很简单。 isPagingEnabled 是由 collectionView 的 frame 决定的,所以我们需要让 collectionView 的 size 比 window 的 frame 大。我们可以只设置 @H_896_5@minimumLinespacing 并设置 collectionView 的布局,使其向右伸展。然后我们会得到两个单元格之间的间隔,页面就可以正常工作了。

代码如下:

layout.itemSize = CGSize(width: UIScreen.main.bounds.size.width,height: UIScreen.main.bounds.size.height)
layout.scrollDirection = .horizontal
layout.minimumLinespacing = 10
view.addSubview(mCollectionView)
mCollectionView.isPagingEnabled = true
mCollectionView.BACkgroundColor = .white
mCollectionView.register(UINib(nibName: "PhotoDetailCVCell",forCellWithReusEIDentifier: cvCellIdentifier)
mCollectionView.snp.makeConsTraints { (makE) in
    make.left.top.bottom.equalToSuperview()
    make.right.equalToSuperview().offset(10)
}

大佬总结

以上是大佬教程为你收集整理的如何使用 Swift 在 iOS 上的 UICollectionView 中设置两张照片之间的间隔?全部内容,希望文章能够帮你解决如何使用 Swift 在 iOS 上的 UICollectionView 中设置两张照片之间的间隔?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。