Ios 带有collectionview的自定义菜单栏

Ios 带有collectionview的自定义菜单栏,ios,swift,uicollectionview,uikit,Ios,Swift,Uicollectionview,Uikit,我想问一下。我从collectionView创建了一个自定义菜单栏,我想在刷卡时链接并更改我的collectionView菜单栏和另一个collectionView的数据。这是我想画的一幅画 但在尝试向左和向右滑动时,我的菜单栏没有跟随,我已经做了一个引用来捕获值。但它仍然不起作用。这是我的密码 class SegmentedView: UIView { let collectionView: UICollectionView = { let layout = UICo

我想问一下。我从collectionView创建了一个自定义菜单栏,我想在刷卡时链接并更改我的collectionView菜单栏和另一个collectionView的数据。这是我想画的一幅画

但在尝试向左和向右滑动时,我的菜单栏没有跟随,我已经做了一个引用来捕获值。但它仍然不起作用。这是我的密码

class SegmentedView: UIView {

    let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        return cv
    }()

    let knowledge = ["Pengetahuan", "Keterampilan", "Sikap"]
    var selectedMenu: CGFloat?

    }

extension SegmentedView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return knowledge.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! SegmentedCell
        let item = knowledge[indexPath.item]
        cell.nameLabel.text = item

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectedMenu = CGFloat(indexPath.item) * frame.width
    }
}

// This from my SegmentedViewController
class SegmentedViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var segmentedViews: SegmentedView!

    let cellId = "assesmentCell"
    let colors: [UIColor] = [UIColor.blue, UIColor.yellow, UIColor.green]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupCollection()
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print(scrollView.contentOffset.x / 3)
        segmentedViews.selectedMenu = scrollView.contentOffset.x / 3
    }

    func setupCollection() {
        if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            flowLayout.scrollDirection = .horizontal
            flowLayout.minimumLineSpacing = 0
        }
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.isPagingEnabled = true
    }
}

谢谢。

您的selectedMenu属性不侦听具有didSet的事件。因此,当您在scrollViewDidScroll中设置selectedMenu时,不会发生任何事情

这可能是一个解决方案:

var selectedMenu: CGFloat? {
    didSet {
        collectionView.selectItem(at: <#T##IndexPath?#>, animated: <#T##Bool#>, scrollPosition: <#T##UICollectionView.ScrollPosition#>)
    }
}
但是要小心分段视图的collectionView:didSelectItemAt:方法,它可能会带来一些不需要的行为。可以使用委托模式在另一个类中触发该方法