SwiftscrollToItem 工作起来很奇怪

问题描述 投票:0回答:2

一开始像视频中那样滑动,效果很好。但是当我单击

next
按钮转到下一页时,单元格会向上移动。

视频

UICollectionViewController类:

private var pageControl: UIPageControl = {
    let pc = UIPageControl()
    pc.currentPage = 0
    pc.numberOfPages = 3
    pc.currentPageIndicatorTintColor = .black
    pc.pageIndicatorTintColor = UIColor.rgb(red: 172, green: 172, blue: 172)
    return pc
}()


override func viewDidLoad() {
    super.viewDidLoad()
    
    self.title = "Test"
    
    collectionView.backgroundColor = .white
    collectionView.register(UserQuickGuideCell.self, forCellWithReuseIdentifier: "cellId")
    collectionView.isPagingEnabled = true
    
    setupPageControl()
}

private func setupPageControl() {
    self.view.addSubview(pageControl)
    self.pageControl.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        self.pageControl.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
        self.pageControl.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        self.pageControl.heightAnchor.constraint(equalToConstant: 50)
    ])
}

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
}

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


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! UserQuickGuideCell
    cell.listener = self
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: view.safeAreaLayoutGuide.layoutFrame.size.height )
}

override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    let x = targetContentOffset.pointee.x
    pageControl.currentPage = Int(x / view.frame.width)
}

func nextButtonOnClicked() {
    if ( pageControl.currentPage == 2 ) {
        
    } else {

        let nextIndex = pageControl.currentPage + 1
        let indexPath = IndexPath(item: nextIndex, section: 0)
        pageControl.currentPage = nextIndex
        collectionView?.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false)
    }
}
swift
2个回答
1
投票

首先返回集合视图框架的大小,不需要传递视图框架。当集合视图框架发生变化时,它将为您提供帮助。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return collectionView.frame.size
}

此外,带有

.isPagingEnabled
的scrollToItem 在 iOS 14 中被破坏。您可以使用此解决方法。

collectionView?.isPagingEnabled = false
collectionView?.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
collectionView?.isPagingEnabled = true

0
投票

使用时

CompositionalLayout
请勿同时设置

collectionView.isPagingEnabled = true

设置您的收藏并且

let section = NSCollectionLayoutSection(group: group)
section.orthogonalScrollingBehavior = .paging

在 UICollectionViewCompositionalLayout 设置中。

© www.soinside.com 2019 - 2024. All rights reserved.