HomeiOS Developmentios - UICollectionView's scrollToItem Points

ios – UICollectionView’s scrollToItem Points


Im attempting to programmatically scroll to a particular index in a UICollectionView from one other UICollectionView, however the scroll will not be occurring.

I need assistance determining what’s inflicting the scrolling to not happen and learn how to repair it. I’ve already confirmed that the code to set off the scroll is being known as and that the right index is being handed in. I additionally checked that the contentOffset values usually are not altering after the tried scroll.
I would like help in figuring out potential the explanation why the scrolling will not be working and learn how to resolve them. Thank You

class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, MenuControllerDelegate {
    
    func didTapMenuItem(indexPath: IndexPath) {
        collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
    }
    
    fileprivate let menuController = MenuController(collectionViewLayout: UICollectionViewFlowLayout())

    override func viewDidLoad() {
        tremendous.viewDidLoad()

        
        menuController.delegate = self
        menuController.collectionView.selectItem(at: [0,0], animated: true, scrollPosition: .centeredHorizontally)
        
        if let format = collectionViewLayout as? UICollectionViewFlowLayout {
            format.scrollDirection = .horizontal
            format.minimumLineSpacing = 0
         
        }
        
        setupLayout()
        
        collectionView.register(MainCell.self, forCellWithReuseIdentifier: "fundamental")
        collectionView.isPagingEnabled = true
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.allowsSelection = true
    }
    fileprivate func setupLayout(){
        view.addSubview(menuController.view)
        
        menuController.view.anchor(high: view.safeAreaLayoutGuide.topAnchor, main: view.leadingAnchor, backside: nil, trailing: view.trailingAnchor, padding: .init(high: 0, left: 0, backside: 0, proper: 0), measurement: .init(width: 0, peak: 70))
        
        collectionView.anchor(high: menuController.view.bottomAnchor, main: view.leadingAnchor, backside: view.bottomAnchor, trailing: view.trailingAnchor, padding: .init(high: 0, left: 0, backside: 0, proper: 0))
    }
    
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let x = scrollView.contentOffset.x
        let offset = x / 2
        menuController.menuBar.remodel = CGAffineTransform(translationX: offset, y: 0)
    }

    override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        let x = targetContentOffset.pointee.x
        let merchandise = x / view.body.width
        let indexPath = IndexPath(merchandise: Int(merchandise), part: 0)
        menuController.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredHorizontally)
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
        return 2
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "fundamental", for: indexPath) as! MainCell
        
        if indexPath.merchandise == 0 {
            cell.backgroundColor = .blue
        }else if indexPath.merchandise == 1 {
            cell.backgroundColor = .inexperienced
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, format collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let peak = view.body.peak
        let width = view.body.width
        return .init(width: width, peak: peak - 70)
    }
}
protocol MenuControllerDelegate {
    
    func didTapMenuItem(indexPath: IndexPath)
}
class MenuController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
    
    fileprivate let menuItems = ["Today", "News"]
    
    var delegate: MenuControllerDelegate?
    
    let menuBar: UIView = {
        let v = UIView()
        v.backgroundColor = .systemBlue
        v.layer.cornerRadius = 35
       return v
    }()

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        collectionView.backgroundColor = .darkGray
        collectionView.layer.cornerRadius = 35
        
        collectionView.register(MenuCell.self, forCellWithReuseIdentifier: "cell")
        
        if let format = collectionViewLayout as? UICollectionViewFlowLayout {
            format.scrollDirection = .horizontal
            format.minimumLineSpacing = 0
            format.minimumInteritemSpacing = 0
        }
        
        
        view.addSubview(menuBar)
        menuBar.anchor(high: view.topAnchor, main: view.leadingAnchor, backside: view.bottomAnchor, trailing: nil, measurement: .init(width: 0, peak: 70))
        menuBar.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1/2).isActive = true
 
    }
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let x = view.body.width / 2 * CGFloat(indexPath.merchandise)
        UIView.animate(withDuration: 0.5, delay: 0,usingSpringWithDamping: 1, initialSpringVelocity: 1, choices: .curveEaseOut, animations: {
            
            self.menuBar.remodel = CGAffineTransform(translationX: x, y: 0)
        })
        delegate?.didTapMenuItem(indexPath: indexPath)
    }
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
        
        return menuItems.depend
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MenuCell
        cell.label.textual content = menuItems[indexPath.item]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, format collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let width = view.body.width
        return .init(width: width / 2, peak: view.body.peak)
    }
    
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments