一个简单分页控制器

struct ColorSet {
    var normal:UIColor = .white
    var select:UIColor = .black
}
protocol LYSegmentViewProtocol {
    func lysegmentViewDidSelect(_ segement:LYSegmentView, at index:Int)
}
class LYSegmentView: UIView {
    
    var titles = [String]()
    var selectBlock:((Int)->Void)?
    var delegate:LYSegmentViewProtocol?
    
    var titleColor:ColorSet = ColorSet(normal: .white, select: .black)
    var backColor:ColorSet = ColorSet(normal: .black, select: .white)
    var borderColor:UIColor = .lightGray{
        didSet{
            self.layer.borderColor = borderColor.cgColor
        }
    }
    var borderWidth:CGFloat = 0.5{
        didSet{
            self.layer.borderWidth = borderWidth
        }
    }
    var titleFontSize:CGFloat = 0
    
    var selectColors : [UIColor]?
    var originIndex:Int = 0{
        didSet{
            guard let stack = self.viewWithTag(101) as? UIStackView else {return}
            let subs = stack.arrangedSubviews
            if originIndex >= subs.count {return}
            guard let sender = subs[originIndex] as? UIButton else {return}
            lastBtn?.isSelected = false
            lastBtn?.backgroundColor = backColor.normal
            lastBtn = sender
            sender.backgroundColor = selecColor
            sender.isSelected = true
        }
    }
    
    /*********************************************************/
    var selectIndex:Int{
        return (lastBtn?.tag ?? 1000) - 1000
    }
    
    /*********************************************************/
    var lastBtn:UIButton?
    var selecColor:UIColor{
        guard let colors = selectColors,colors.count > selectIndex else {return backColor.select}
        return colors[selectIndex]
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    init() {
        super.init(frame: .zero)
    }

    func initViews(){
        _ = self.subviews.map{$0.removeFromSuperview()}
        assert(titles.count != 0, "titles 不能为空")

        var list = [UIButton]()
        for (index,title) in titles.enumerated(){
            let btn = UIButton(type: .custom)
            btn.setTitle(title, for: .normal)
            btn.setTitle(title, for: .selected)
            btn.setTitle(title, for: .highlighted)
            btn.tag = 1000 + index
            btn.setTitleColor(titleColor.normal, for: .normal)
            btn.setTitleColor(titleColor.select, for: .selected)
            btn.titleLabel?.numberOfLines = 0
            btn.titleLabel?.lineBreakMode = .byWordWrapping
            if titleFontSize > 0{
                btn.titleLabel?.font = UIFont.systemFont(ofSize: titleFontSize)
            }else{
                btn.titleLabel?.adjustsFontSizeToFitWidth = true
            }
            if index == originIndex{
                btn.isSelected = true
                btn.backgroundColor = selecColor
                lastBtn = btn
            }else{
                btn.backgroundColor = backColor.normal
            }
            
            btn.addTarget(self, action: #selector(self.btnClickAction(_:)), for: .touchUpInside)
            
            list.append(btn)
        }
        
        let stack = UIStackView(arrangedSubviews: list)
        stack.distribution = .fillEqually
        stack.axis = .horizontal
        stack.tag = 101
        self.addSubview(stack)
        stack.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.layer.cornerRadius = 5.5
        self.clipsToBounds = true
        self.layer.borderWidth = borderWidth
        self.layer.borderColor = borderColor.cgColor
    }
    
    
    @objc func btnClickAction(_ sender:UIButton){
        if sender == lastBtn{return}
        lastBtn?.isSelected = false
        lastBtn?.backgroundColor = backColor.normal
        lastBtn = sender
        sender.backgroundColor = selecColor
        sender.isSelected = true
        self.selectBlock?(selectIndex)
        self.delegate?.lysegmentViewDidSelect(self, at: selectIndex)
    }
    
    func refreshTitles(){
        guard let stack = self.viewWithTag(101) as? UIStackView else {return}
        guard let subs = stack.arrangedSubviews as? [UIButton] else {return}
        if subs.count != titles.count {return}
        for i in 0..= titles.count {continue}
            let title = titles[index]
            btn.setTitle(title, for: .normal)
            btn.setTitle(title, for: .selected)
            btn.setTitle(title, for: .highlighted)
            btn.titleLabel?.numberOfLines = 0
            if titleFontSize > 0{
                btn.titleLabel?.font = UIFont.systemFont(ofSize: titleFontSize)
            }
            if btn.isSelected{
                btn.backgroundColor = self.selecColor
            }else{
                btn.backgroundColor = backColor.normal
            }
        }
    }
        
    override var intrinsicContentSize: CGSize{
        return UIView.layoutFittingCompressedSize
    }
}

你可能感兴趣的:(一个简单分页控制器)