gpt4 book ai didi

swift - 将 IGListKit 用于带分页的水平列表

转载 作者:行者123 更新时间:2023-11-28 15:00:25 26 4
gpt4 key购买 nike

我正在构建一个严重依赖 Collection View 的应用程序,因此我决定使用 IGListKit。这是我必须做的:

enter image description here

这是我所拥有的:

enter image description here...而且它没有朝任何方向移动!

这是我的代码:

import UIKit
import IGListKit

class MatchCollectionViewCell: UICollectionViewCell {
var helloWorld = "Hello World"
}

class LabelSectionController: ListSectionController {
override func sizeForItem(at index: Int) -> CGSize {
return CGSize(width: collectionContext!.containerSize.width, height: 55)
}

override func cellForItem(at index: Int) -> UICollectionViewCell {
return collectionContext!.dequeueReusableCell(of: MatchCollectionViewCell.self, for: self, at: index)
}
}

class MatchViewController: UIViewController {

// MARK: - Outlets
@IBOutlet weak var matchCollectionView: UICollectionView!


// MARK: - Variables
let layout = UICollectionViewFlowLayout()
lazy var adapter: ListAdapter = {
return ListAdapter(updater: ListAdapterUpdater(), viewController: self, workingRangeSize: 0)
}()
var users: [User] = []

// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
matchCollectionView.dataSource = self
matchCollectionView.delegate = self
matchCollectionView.collectionViewLayout = CenterCellCollectionViewFlowLayout()
matchCollectionView.isPagingEnabled = true
layout.minimumLineSpacing = 8
layout.scrollDirection = .horizontal

for i in 1...10 {
let newMatchingPreferences = MatchingPreferences(preferedAge: (23, 33))
let newUser = User(id: i, name: "Some Name \(i)", email: "some_name@gmail.com", age: 27, location: "New York", isOnboarded: true, isPremium: true, matchingPreferences: newMatchingPreferences)
users.append(newUser)
}
}

// MARK: - Actions

// MARK: - Methods
}

extension MatchViewController: ListAdapterDataSource {
func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
return self.users
}

func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
return LabelSectionController()
}

func emptyView(for listAdapter: ListAdapter) -> UIView? {
let view = UIView()
view.backgroundColor = .lightGray
return view
}
}

extension MatchViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = matchCollectionView.dequeueReusableCell(withReuseIdentifier: "MatchCell", for: indexPath) as! MatchCollectionViewCell
print(cell.helloWorld)
return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.bounds.width - 32, height: view.bounds.height - 40)
}


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

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}

class CenterCellCollectionViewFlowLayout: UICollectionViewFlowLayout {
var mostRecentOffset: CGPoint = CGPoint.zero

override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {

if velocity.x == 0 {
return self.mostRecentOffset
}

guard let cv = self.collectionView,
let attributesForVisibleCells = self.layoutAttributesForElements(in: cv.bounds) else {
// Fallback
self.mostRecentOffset = super.targetContentOffset(forProposedContentOffset: proposedContentOffset)
return self.mostRecentOffset
}

let halfWidth = cv.bounds.size.width * 0.5

var candidateAttributes: UICollectionViewLayoutAttributes?
for attributes in attributesForVisibleCells {

// Skip comparison with non-cell items (headers and footers)
if attributes.representedElementCategory != UICollectionElementCategory.cell {
continue
}

if (attributes.center.x == 0) || (attributes.center.x > (cv.contentOffset.x + halfWidth) && velocity.x < 0) {
continue
}

candidateAttributes = attributes
}

// Beautification step , I don't know why it works!
if proposedContentOffset.x == -(cv.contentInset.left) {
return proposedContentOffset
}

guard let attributes = candidateAttributes else {
return mostRecentOffset
}

self.mostRecentOffset = CGPoint(x: floor(attributes.center.x - halfWidth), y: proposedContentOffset.y)

return self.mostRecentOffset
}
}

基本上,我知道如何制作粗略的 Collection View ,但分页和居中对我来说有点难。我觉得我很接近,但显然缺少一些东西。我希望有人能指出我正确的方向!谢谢。

最佳答案

    extension ViewController: UIScrollViewDelegate {
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if scrollView == self.collectionView {
var currentCellOffset = self.collectionView.contentOffset
currentCellOffset.x += self.collectionView.frame.width / 2
if let indexPath = self.collectionView.indexPathForItem(at: currentCellOffset) {
self.collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
}
}
}

确保设置

adapter.scrollDelegate = self

collectionView.decelerationRate = UIScrollViewDecelerationRateFast

(更多原生 iOS 的感觉)

这对我有用,希望对其他人有帮助!

关于swift - 将 IGListKit 用于带分页的水平列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48967298/

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