IOS横线滚动视图的实现---方式一


 lazy var  scrollView = OKScrollView()

//初始化

scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        self.view.addSubview(scrollView)
        scrollView.backgroundColor = OKColor(hexRGB: 0xF5F5F5)
        scrollView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(0)
            make.height.equalTo(28)
        }

//添加子控件

 var lastBtn: OKButton?
            for (index, title) in self.mNewsTab.enumerated() {
                let btn = OKButton(type: .custom)
                btn.tag = index
                btn.setTitle(title.name, for: .normal)
                btn.setTitleColor(OKColor(hexRGB: 0x595757), for: .normal)
                btn.setTitleColor(OKColor(hexRGB: 0x4594E9), for: .selected)
                btn.titleLabel?.font = OKFont.systemFont(size: 10)
                btn.addTarget(self, action: #selector(self.selectedAction(_:)), for: .touchUpInside)
                self.scrollView.addSubview(btn)
                if index == 0 {

                    btn.isSelected = true
                    self.currClickButton = btn
                    
                    self.mType = title.code
                    self.loadData(more: false)
                }
                btn.snp.makeConstraints({ (make) in
                    make.top.bottom.equalToSuperview()
                    make.size.equalTo(CGSize(width: 60, height: 28))
                    if let lastBtn = lastBtn {
                        make.leading.equalTo(lastBtn.snp.trailing)
                    } else {
                        make.leading.equalToSuperview()
                    }
                    if index == self.mNewsTab.count - 1 {
                        make.trailing.equalToSuperview()
                    }
                })
                lastBtn = btn
            }

//点击交互
 var currClickButton:OKButton = OKButton(type: .custom)
    @objc
    private func selectedAction(_ sender: OKButton) {
        currClickButton.isSelected = false
        
        sender.isSelected = true
        currClickButton = sender
        
        self.mType = mNewsTab[sender.tag].code
        self.loadData(more: false)
    }

你可能感兴趣的:(IOS横线滚动视图的实现---方式一)