使用按钮来回滚动多次

scroll back and forth multiple times with the buttons

前后按钮只能使用一次,我无法使用按钮多次来回滚动。

我试图在每次点击按钮时单元格不再可见后将集合视图滚动到下一个单元格。

我现在的代码不能正常工作。

我该如何解决这个问题,以便在我点击按钮时它可以正确滚动?

代码:


import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    
    var labelArray = ["User1", "User2", "User3", "User4", "User5", "User6", "User7", "User8", "User9"]
    
    var numberLeft = -200.0
    var numberRight = 200.0
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    
   override func viewDidLoad() {
        super.viewDidLoad()
    
    collectionView.delegate = self
    collectionView.dataSource = self
    
    print("NUMBERLEFT: \(numberLeft) - NUMBERRIGHT: \(numberRight)")
    
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return labelArray.count
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
        
        cell.label.text = labelArray[indexPath.row]
        

       
       collectionView.contentInset = UIEdgeInsets(top: 0, left: CGFloat(numberLeft), bottom: 0, right: CGFloat(numberRight))
        
        print("NUMBERLEFT: \(numberLeft) - NUMBERRIGHT: \(numberRight)")
        
        return cell
        
    }

    
    
    
    @IBAction func buttonLeft(_ sender: Any) {
        numberLeft = numberLeft + 100.0
        collectionView.reloadData()
     }
    
    @IBAction func buttonRight(_ sender: Any) {
        numberRight = numberRight + 100.0
        collectionView.reloadData()
    }
    
   
    

}

enter image description here

如果您想滚动集合视图,那么您应该使用函数 scrollRectToVisible...https://developer.apple.com/documentation/uikit/uiscrollview/1619439-scrollrecttovisible

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    
    var labelArray = ["User1", "User2", "User3", "User4", "User5", "User6", "User7", "User8", "User9"]
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        collectionView.delegate = self
        collectionView.dataSource = self
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return labelArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
        
        cell.label.text = labelArray[indexPath.row]
        
        return cell
    }

    // Change the button actions to just scroll the collection view rather than having o reload it.
    @IBAction func buttonLeft(_ sender: Any) {
        collectionView.scrollRectToVisible(collectionView.visibleRect.offsetBy(dx: -100, dy: 0), animated: true)
    }
    
    @IBAction func buttonRight(_ sender: Any) {
        collectionView.scrollRectToVisible(collectionView.visibleRect.offsetBy(dx: 100, dy: 0), animated: true)
    }
}

extension UIScrollView {
    var visibleRect: CGRect {
        CGRect(origin: contentOffset, size: bounds.size)
    }
}

反正就是这样。您可能想要调整左右滚动的量。