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{
let btn = subs[i]
let index = btn.tag - 1000
if index >= 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
}
}
一个简单分页控制器
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...