swift、iOS自定义通用可切换tab菜单页面

iOS自定义通用可切换tab菜单页面

tab菜单页代码

import UIKit
let DQScreenWidth:CGFloat = UIScreen.main.bounds.size.width
let DQScreenHeight:CGFloat = UIScreen.main.bounds.size.height
let DQTitleFont:UIFont = UIFont.systemFont(ofSize: 15)//标题的大小
let DQTitleHeight:CGFloat = 50.0//标题滚动视图的高度
let DQUnderlineHeight:CGFloat = 4.0//自定义滑动条的高度
let DQColer:UIColor = UIColor(red: 14/255, green: 193/255, blue: 243/255, alpha: 1) //标题选中的颜色
let DQNavigationHeight:CGFloat = 0//导航栏的高度
let DQTabbarHeight:CGFloat = 50.0//Tabbar的高度
let DQButtonStartTag:Int = 2000

class DQBaseHomeViewController: UIViewController,UIScrollViewDelegate{
    
    var titleScrollView:UIScrollView?//标题滚动视图
    var contentScollView:UIScrollView?//管理子控制器View的滚动视图
    var selectButton:UIButton?//保存选中的按钮
    var titleSButtons:NSMutableArray = NSMutableArray.init()//保存标题按钮的数组
    var selectIndex:Int = 0//选中的下标
    var isIninttial:Bool = false//第一次加载的变量
    let btnScale:CGFloat = 0.0//用于做过度的效果
    var underline:UIView?//自定义的滑动条
    var willDisapperIndex = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        automaticallyAdjustsScrollViewInsets = false
        view.backgroundColor = UIColor.white
        setupTitleScrollViewFunction()
        setupContentScrollVewFunction()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if !isIninttial {
            setupAllButtonTitle()
            setupUnderlineFunction()
        }
        selectIndex = willDisapperIndex
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        selectIndex = willDisapperIndex
        isIninttial = true
    }
    
    
    func setupTitleScrollViewFunction() -> Void{
        titleScrollView = UIScrollView.init(frame: CGRect(x: 0, y: DQNavigationHeight, width: DQScreenWidth, height: DQTitleHeight))
        titleScrollView?.showsHorizontalScrollIndicator = false
        titleScrollView?.scrollsToTop = false
        titleScrollView?.backgroundColor = UIColor.white
        self.view.addSubview(titleScrollView!)
    
    }
    func setupAllButtonTitle() -> Void {
        
        let count:Int = self.childViewControllers.count
        let btnW:CGFloat = DQScreenWidth/CGFloat(count)
        titleSButtons.removeAllObjects()
        for  i  in 0..<count {
            let button:UIButton = UIButton.init(type: .custom)
            let btnX:CGFloat = CGFloat(i)*btnW
            button.frame = CGRect(x: btnX, y: 0, width: btnW, height: DQTitleHeight-5)
            button.tag = DQButtonStartTag+i
            button.titleLabel?.font = DQTitleFont
            let vc:UIViewController = self.childViewControllers[i]
            button.setTitleColor(UIColor(red: 51/255, green: 51/255, blue: 51/255, alpha: 1), for: .normal)
            button.setTitleColor(UIColor(red: 14/255, green: 193/255, blue: 243/255, alpha: 1), for: .selected)
            button.setTitle(vc.title, for: .normal)
            titleScrollView?.addSubview(button)
            titleSButtons.add(button)
            button.addTarget(self, action: #selector(titleclickAction(sender:)), for: .touchUpInside)
            
        }
        dealBtnClickAction(sender: titleSButtons[selectIndex] as! UIButton)
        
        titleScrollView?.contentSize = CGSize.init(width: CGFloat(count)*btnW, height: DQTitleHeight)
        contentScollView?.contentSize = CGSize.init(width: CGFloat(count)*DQScreenWidth, height: DQScreenWidth-DQTitleHeight-DQNavigationHeight)
        
    }
    func setupUnderlineFunction(){
        if underline != nil {
            self.underline?.removeFromSuperview()
        }
        let firstTitleButton:UIButton = titleScrollView?.subviews[selectIndex] as! UIButton
        
        let underlineView = UIView.init()
        underlineView.frame = CGRect.init(x: 0, y: DQTitleHeight-DQUnderlineHeight, width: 70, height: DQUnderlineHeight)
        underlineView.backgroundColor = DQColer
        titleScrollView?.addSubview(underlineView)
        underline = underlineView
        
        firstTitleButton.titleLabel?.sizeToFit()
        
        underline?.frame.size.width = (firstTitleButton.titleLabel?.frame.size.width)! + 10
        underline?.center.x = firstTitleButton.center.x
        
        let lineLayer = UIView.init()
        lineLayer.backgroundColor = UIColor.init(red: 0.93, green: 0.93, blue: 0.93, alpha: 1.0)
        lineLayer.frame = CGRect.init(x: 0, y: DQTitleHeight-1, width: DQScreenWidth, height: 1)
        titleScrollView?.addSubview(lineLayer)
    }
    func setupContentScrollVewFunction() -> Void {
        contentScollView = UIScrollView.init(frame: CGRect.init(x: 0, y: DQTitleHeight+DQNavigationHeight, width: DQScreenWidth, height: DQScreenHeight-DQTitleHeight-DQTabbarHeight-DQNavigationHeight))
        self.view.insertSubview(contentScollView!, at: 0)
        contentScollView?.backgroundColor = UIColor.white
        contentScollView?.showsHorizontalScrollIndicator = false
        contentScollView?.isPagingEnabled = true
        contentScollView?.bounces = false
        contentScollView?.alwaysBounceVertical = false
        contentScollView?.scrollsToTop = false
        contentScollView?.delegate = self
    }
    
    func titleclickAction(sender:UIButton) -> Void {
          dealBtnClickAction(sender: sender)
    }
    
    func setupTitleCenterFunction(sender:UIButton) -> Void {
        var offsetX:CGFloat = sender.center.x - DQScreenWidth*0.5
        if offsetX<0 {
            offsetX = 0
        }
        
        let maxoffsetX = (titleScrollView?.contentSize.width)! - DQScreenWidth
        if offsetX>maxoffsetX {
            offsetX = maxoffsetX
        }
        
        titleScrollView?.setContentOffset(CGPoint.init(x: offsetX, y: 0), animated: true)
        
    }
    
    func setupOneChildViewController(index:Int) -> Void {
        if index>=self.childViewControllers.count {
            return
        }
        let vc:UIViewController = self.childViewControllers[index]
        if (vc.view.superview != nil) {
            return
        }
        
        let offX = CGFloat(index)*DQScreenWidth
        vc.view.frame = CGRect.init(x: offX, y: 0, width: DQScreenWidth, height: (contentScollView?.frame.size.height)!)
        contentScollView?.addSubview(vc.view)
        
        
    }
    func adjustUnderLine(sender:UIButton) -> Void {
        underline?.frame.size.width = (sender.titleLabel?.frame.size.width)!+10
        underline?.center.x = sender.center.x
        
    }
    func selectTitleButton(sender:UIButton) -> Void {
        selectButton?.setTitleColor(UIColor(red: 51/255, green: 51/255, blue: 51/255, alpha: 1), for: .normal)
        sender.setTitleColor(UIColor(red: 14/255, green: 193/255, blue: 243/255, alpha: 1), for: .normal)
        let scale:CGFloat = 1 + btnScale
        
        selectButton?.transform = CGAffineTransform.identity
        sender.transform = CGAffineTransform.init(scaleX: scale, y: scale)

        selectButton = sender
    }
    func dealBtnClickAction(sender:UIButton) -> Void {
        let index = sender.tag - DQButtonStartTag
        //willDisapperIndex = index
        //selectIndex = willDisapperIndex
        selectTitleButton(sender: sender)
        setupOneChildViewController(index: index)
        contentScollView?.contentOffset = CGPoint.init(x: CGFloat(index)*DQScreenWidth, y: 0)
        willDisapperIndex = index
        UIView.animate(withDuration: 0.25) { 
            
            self.adjustUnderLine(sender: sender)
            
        }
        for i in 0..<titleSButtons.count{
            if !(i==index){
                let noSelectBtn:UIButton = titleSButtons[i] as! UIButton
                noSelectBtn.setTitleColor(UIColor(red: 51/255, green: 51/255, blue: 51/255, alpha: 1), for: .normal)
            }
        }
        
        for i in 0..<childViewControllers.count{
            let chilaCtl:UIViewController = self.childViewControllers[i]
            if !chilaCtl.isViewLoaded {
                continue
            }
        
            let childVcView:UIView = chilaCtl.view
            if childVcView.isKind(of: UIScrollView.classForCoder()) {
                let scrollView:UIScrollView = childVcView as! UIScrollView
                scrollView.scrollsToTop = (i == index)
                if i == index {
                    
                }
            }
            
        }
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let value = scrollView.contentOffset.x/DQScreenWidth
        let leftIndex:Int = Int(value)
        let rightIndex = leftIndex+1
        let scaleRight:CGFloat = scrollView.contentOffset.x/DQScreenWidth - CGFloat(leftIndex)
        let scaleLeft:CGFloat = 1 - scaleRight
        let leftTitleBtn:UIButton = self.titleSButtons[leftIndex] as! UIButton
        leftTitleBtn.dqButtonColorScale(scaleLeft)
        
        if rightIndex < self.titleSButtons.count {
            let rightTitleBtn:UIButton = self.titleSButtons[rightIndex] as! UIButton
            rightTitleBtn.dqButtonColorScale(scaleRight)
            rightTitleBtn.transform = CGAffineTransform.init(scaleX: scaleRight*self.btnScale+1, y: scaleRight*self.btnScale+1)
            
        }
        
        setupOneChildViewController(index: rightIndex)
    }
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let index:Int = Int(scrollView.contentOffset.x/DQScreenWidth)
        let button:UIButton = titleSButtons[index] as! UIButton
        willDisapperIndex = index
        dealBtnClickAction(sender: button)
    }
}

使用

import UIKit
import SnapKit

let GNScreenW = UIScreen.main.bounds.size.width
let GNScrennH = UIScreen.main.bounds.size.height

class CollectListViewController: DQBaseHomeViewController{
    
    let ctl1 = CommonCollectionViewController()
    let ctl2 = CommonCollectionViewController()
    let ctl3 = CommonCollectionViewController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "清单"
        
        ctl1.title = "收藏商品"
        ctl1.view.backgroundColor = UIColor.white
        
        ctl2.title = "常购商品"
        ctl2.view.backgroundColor = UIColor.white
        
        ctl3.title = "浏览记录"
        ctl3.view.backgroundColor = UIColor.white
        
        self.addChildViewController(ctl1)
        self.addChildViewController(ctl2)
        self.addChildViewController(ctl3)
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}

效果图

swift、iOS自定义通用可切换tab菜单页面_第1张图片

你可能感兴趣的:(swift,iOS,swift,iOS,自定义tab菜单,可切换菜单)