美文网首页
今天简单封装了一个不错分页控制器

今天简单封装了一个不错分页控制器

作者: wlysky | 来源:发表于2017-08-09 17:51 被阅读18次

    //MARK:WARNNING--如果视图里面存在唯一一个UIScrollView或其子类View,那么它会自动设置相应的内边距,有鬼😂,请关闭viewcontroller的automaticallyAdjustsScrollViewInsets属性automaticallyAdjustsScrollViewInsets

    classLYSegementView:UIView{

    varlineView =UIView(frame:CGRect(x:0, y:0, width:100, height:2))

    varfontSize :CGFloat=15

    privatevarscro =UIScrollView()

    vartitles = [String]()

    vartapCloser:((_item:Int)->Void)?

    varlineColor =UIColor.orange

    varlineHeight :CGFloat=2

    varspaceColor =UIColor.darkGray

    varspaceTopAndBottom:CGFloat=0

    varspaceWidth :CGFloat=0//控件间隙

    vartitleBgColor =UIColor.white

    vartitleColor =UIColor.black

    vartitleSelectColor =UIColor.orange//title选中颜色

    varanimationTime =0.25//动画时间lineView移动动画时间

    varselectIndex =0{//外部改变当前选中,超出不做反应

    didSet{

    ifselectIndex!= oldValue&&selectIndex

    lineOutEffectAction(btns[selectIndex])

    }

    }

    }

    varcurrentSelect =0

    privatevarbtns = [UIButton]()//all of the title Btn

    // Stores the current view size to detect whether a redraw is needed in layoutSubviews

    varcurrentViewSize =CGSize()

    varlastSelect =UIButton()

    init() {

    super.init(frame:CGRect(x:0, y:0, width:0, height:0))

    }

    requiredinit?(coder aDecoder:NSCoder) {

    super.init(coder: aDecoder)

    }

    overrideinit(frame:CGRect) {

    super.init(frame: frame)

    }

    init(frame:CGRect,titles:[String]){

    super.init(frame: frame)

    self.titles= titles

    }

    init(frame:CGRect,titles:[String],block:@escaping((_item:Int) ->Void)){

    super.init(frame: frame)

    self.titles= titles

    tapCloser= block

    }

    overridefunclayoutSubviews() {

    super.layoutSubviews()

    ifcurrentViewSize==self.frame.size{

    return

    }

    currentViewSize=self.frame.size

    commonInit(frame:self.frame, titles:titles, block:tapCloser)

    }

    funccommonInit(frame:CGRect,titles:[String],block:((_item:Int) ->Void)?){

    scro.frame=self.bounds

    //scro.showsVerticalScrollIndicator = false

    //scro.showsHorizontalScrollIndicator = false

    self.addSubview(scro)

    lineView.backgroundColor=lineColor

    lineView.frame.size.height=lineHeight

    iftitles.count==0{

    return

    }

    vartotalWidth:CGFloat=0//宽度

    //let spaceWidth : CGFloat = 2

    vartotalSize :CGFloat=0

    //计算所有标题在15号字体下的宽度

    forstrintitles{

    totalWidth +=CGFloat(str.characters.count) *fontSize

    totalSize +=CGFloat(str.characters.count)

    }

    totalWidth += (CGFloat(titles.count-1) *spaceWidth)

    letsizeWidth = totalWidth >UIScreen.main.bounds.width? totalWidth :UIScreen.main.bounds.width

    scro.contentSize=CGSize(width: sizeWidth, height: frame.height)

    foriin0..

    letstr = titles[i]

    varitemWidth :CGFloat=0// CGFloat(str.characters.count) * fontSize

    iftotalWidth >UIScreen.main.bounds.width{

    itemWidth =CGFloat(str.characters.count) *fontSize

    }else{

    itemWidth = (UIScreen.main.bounds.width-spaceWidth*CGFloat(titles.count-1))/totalSize *CGFloat(str.characters.count)

    }

    letbtn =UIButton(type: .custom)

    scro.addSubview(btn)

    btn.titleLabel?.font=UIFont.systemFont(ofSize:fontSize)

    btn.setTitle(str, for: .normal)

    btn.frame=CGRect(x:CGFloat(i) * (itemWidth +spaceWidth), y:0, width: itemWidth, height: frame.height)

    btn.setTitleColor(titleColor, for: .normal)

    btn.setTitleColor(titleSelectColor, for: .selected)

    btn.setTitleColor(titleColor, for: .highlighted)

    btn.backgroundColor=titleBgColor

    btn.addTarget(self, action:#selector(self.titleAction(_:)), for: .touchUpInside)

    btn.tag= i

    btns.append(btn)

    ifi >0{

    letspace =UIView(frame:CGRect(x:CGFloat(i)*itemWidth +spaceWidth*CGFloat(i -1), y:spaceTopAndBottom, width:spaceWidth, height: frame.height-spaceTopAndBottom*2))

    space.backgroundColor=spaceColor

    scro.addSubview(space)

    }

    ifi ==0{

    lineView.frame.size.width= btn.frame.width

    btn.isSelected=true

    lastSelect= btn

    }

    }

    lineView.frame.origin.y= frame.height-lineView.frame.height

    scro.addSubview(lineView)

    scro.contentOffset=CGPoint(x:0, y:0)

    }

    functitleAction(_sender:UIButton){

    lastSelect.isSelected=false

    sender.isSelected=true

    lastSelect= sender

    currentSelect= sender.tag

    tapCloser?(sender.tag)

    iflineView.frame.origin.x!= sender.frame.origin.x{

    UIView.animate(withDuration:animationTime, animations: {

    self.lineView.frame.origin.x= sender.frame.origin.x

    self.lineView.frame.size.width= sender.frame.width

    })

    }

    }

    //外部改变selectIndex调用,仅做lineView移动动画处理

    funclineOutEffectAction(_sender:UIButton){

    lastSelect.isSelected=false

    sender.isSelected=true

    lastSelect= sender

    currentSelect= sender.tag

    iflineView.frame.origin.x!= sender.frame.origin.x{

    UIView.animate(withDuration:animationTime, animations: {

    self.lineView.frame.origin.x= sender.frame.origin.x

    self.lineView.frame.size.width= sender.frame.width

    })

    }

    }

    }

    相关文章

      网友评论

          本文标题:今天简单封装了一个不错分页控制器

          本文链接:https://www.haomeiwen.com/subject/gyuqrxtx.html