gpt4 book ai didi

ios - 如何使用 UICollectionViewFlowLayout 将集合中的每个 View 居中?

转载 作者:行者123 更新时间:2023-11-29 05:55:58 27 4
gpt4 key购买 nike

这里发布了类似的问题,但问题依赖于我的代码,我不知道如何解决它。

我不知道为什么,但当我滚动我的 collectionview 时,单元格向左移动得更多。见下图:

enter image description here

这是我的 UICollectionViewFlowLayout 的代码

import UIKit

class PrayerFlowLayout: UICollectionViewFlowLayout {
//let standardItemAlpha: CGFloat = 0.3
let standardItemScale: CGFloat = 0.85

var isSetup = false

override func prepare() {
super.prepare()
if isSetup == false {
setupCollectionView()
isSetup = true
}
}

override open func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributes = super.layoutAttributesForElements(in: rect)
var attributesCopy = [UICollectionViewLayoutAttributes]()

for itemAttributes in attributes! {
let itemAttributesCopy = itemAttributes.copy() as! UICollectionViewLayoutAttributes

changeLayoutAttributes(itemAttributesCopy)
attributesCopy.append(itemAttributesCopy)
}

return attributesCopy
}


// indicates the point on where to stop scrolling each prayer
override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
// get layout attribute to use to make some calculations
let layoutAttributes = self.layoutAttributesForElements(in: collectionView!.bounds)

// get the horizontal center on the collection
let center = collectionView!.frame.size.width / 2

// add the center to the proposed content offset
let proporsedContentOffsetCenterOrigin = proposedContentOffset.x + center

let closest = layoutAttributes!.sorted {
abs($0.center.x - proporsedContentOffsetCenterOrigin) < abs($1.center.x - proporsedContentOffsetCenterOrigin)
}.first ?? UICollectionViewLayoutAttributes()

let targetContentOffset = CGPoint(x: floor(closest.center.x - center), y: proposedContentOffset.y - center)

return targetContentOffset
}

func changeLayoutAttributes(_ attributes: UICollectionViewLayoutAttributes) {
let collectionCenter = collectionView!.bounds.width / 2
let offset = collectionView!.contentOffset.x
let normalizedCenter = attributes.center.x - offset

let maxDistance = collectionView!.bounds.size.width + self.minimumLineSpacing
//collectionView!.frame.width + self.minimumLineSpacing // self.itemSize.width + self.minimumLineSpacing
let distance = min(abs(collectionCenter - normalizedCenter), maxDistance)

let ratio = (maxDistance - distance)/maxDistance

//let alpha = ratio * (1 - self.standardItemAlpha) + self.standardItemAlpha
let scale = ratio * (1 - self.standardItemScale) + self.standardItemScale

//attributes.alpha = alpha
attributes.transform3D = CATransform3DScale(CATransform3DIdentity, scale, scale, 1)
}


func setupCollectionView() {
self.collectionView!.decelerationRate = UIScrollView.DecelerationRate.fast

let collectionSize = collectionView!.bounds.size
let yInset = (collectionSize.height - self.itemSize.height) / 3
let xInset = (collectionSize.width - self.itemSize.width) / 2

let topPos = (collectionView!.bounds.height - (collectionView!.bounds.height - 75) )

self.sectionInset = UIEdgeInsets.init(top: topPos, left: xInset, bottom: yInset, right: xInset)
}

}

关于如何让所有单元格始终居中有什么想法吗?

最佳答案

您需要使用这样的UICollectionViewDelegateFlowLayout方法

class CardListViewController:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{

// UICollectionViewDelegateFlowLayout Delegate method

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let leftAndRightPaddings: CGFloat = 20.0
let numberOfItemsPerRow: CGFloat = 1.0

let width = (collectionView.frame.width - leftAndRightPaddings)/numberOfItemsPerRow
return CGSize(width: width, height: collectionView.frame.width)

}
}

来自 Storyboard 集部分的插图,如下所示。

enter image description here

输出:

enter image description here

关于ios - 如何使用 UICollectionViewFlowLayout 将集合中的每个 View 居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55154997/

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