gpt4 book ai didi

ios - 像 Lyft 应用程序一样集中放置 ImageView

转载 作者:行者123 更新时间:2023-11-30 11:46:44 25 4
gpt4 key购买 nike

我尝试开发一个像Lyft应用程序enter image description here这样的页面

使用了此类 View 的 Collection View ,但没有得到如图所示的预期结果。需要一种将图像集中放置的替代方法,并且它必须是可滚动的

最佳答案

此代码将告诉您如何获取中心点。

我为此做了示例,您可以根据自己的需要自定义此基础。

设置Collectionview scrolling directionHorizontallineSpacing10Storyboard

如果您想显示 20 个项目,请添加 21 行。要获得中间的最后一行。

var numbOfItems : Int = 21

let cellWidth : CGFloat = 70



override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numbOfItems
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! RulesCollectionViewCell
cell.backgroundColor = UIColor.clear

cell.layer.borderColor = UIColor(red: 83/255, green: 50/255, blue: 129/255, alpha: 1.0).cgColor
cell.layer.borderWidth = 1.0
cell.txtLbl.text = String(indexPath.item)
cell.txtLbl.textAlignment = .center
cell.txtLbl.textColor = UIColor.darkGray


cell.layer.cornerRadius = 5
cell.alpha = 1.0

if indexPath.row == numbOfItems - 1
{
cell.alpha = 0.0
}
return cell
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

let leftPadding = (collectionView.frame.size.width / 2) - (cellWidth / 2)
let rightPadding = (collectionView.frame.size.width / 2) - (cellWidth / 2)


return UIEdgeInsets(top: padding, left: leftPadding, bottom: padding, right: rightPadding)
}

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

override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {

getCenterPointOfCollectionViewLogic()
print("Drag_ends")
}

override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

getCenterPointOfCollectionViewLogic()

}


func getCenterPointOfCollectionViewLogic()
{
let contenOff = self.collectionView?.contentOffset
let ip : [IndexPath] = (collectionView?.indexPathsForVisibleItems)!
var itemsArr = [Int]()
var m2 = Int()
var centerIp = IndexPath()
var gettingCell = RulesCollectionViewCell()
var setPointX : CGFloat = 0.0


if (contenOff?.x)! > CGFloat(50.0)
{
for i in 0..<ip.count
{
itemsArr.append(ip[i].item)
}
m2 = Int(calculateMedian(array: itemsArr))

let lastItemPointX = CGFloat(((numbOfItems - 1) * Int(cellWidth)) + ((numbOfItems - 1) * 10)) // THIS "10" is CollectionViewCell's LineSpacing.

print("lastItemPointXlastItemPointX ", lastItemPointX)

if (contenOff?.x)! >= lastItemPointX
{
m2 = m2 + 1
}
setPointX = CGFloat(m2) * cellWidth + CGFloat((m2 * 10))
}
else
{
m2 = 0

setPointX = 0.0
}

centerIp = IndexPath(item: m2, section: 0)
gettingCell = collectionView?.cellForItem(at: centerIp) as! RulesCollectionViewCell
self.collectionView?.setContentOffset(CGPoint(x: setPointX, y: 0.0), animated: true)

}

func calculateMedian(array: [Int]) -> Float {
let sorted = array.sorted()
if sorted.count % 2 == 0 {
return Float((sorted[(sorted.count / 2)] + sorted[(sorted.count / 2) - 1])) / 2
} else {
return Float(sorted[(sorted.count - 1) / 2])
}
}

关于ios - 像 Lyft 应用程序一样集中放置 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48744103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com