大佬教程收集整理的这篇文章主要介绍了如何使用 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)
}
目标效果:
休息时:
当我们移动它时:
而目前的风格仍然是:
这是一种方法 - 它可能适合也可能不适合您的需求...
对于你的单元格布局,给图像视图每边 5 点“填充”:
当单元格相邻时(流布局设置为 .minimumLinespacing = 0
和 .minimumInteritemSpacing = 0
),视觉效果将使它看起来之间有 10 pts 的间距图片:
接下来,对集合视图进行布局,使其比控制器的视图宽:
当您拖动时,单元格之间会出现 10 pts 的间距:
和 .isPagingEnabled = true
会将单元格“对齐”到位:
这是一个完整的示例 - 注意:这是示例代码!!
带有 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,请注明来意。