swift_PagingMenuController分段控件使用

gitHub地址:https://github.com/kitasuke/PagingMenuController 里边有详细的讲解
platform :ios,'9.0'
target ‘SwiftDemo’ do
use_frameworks!
inhibit_all_warnings!
pod 'PagingMenuController'

end

创建5个UIViewController,

设置一个根视图来实现分段控件加载

import PagingMenuController

/// 初始化一个结构体,用来对分段视图控件配置
private struct pagInMenuOptions : PagingMenuControllerCustomizable
{
    //初始化一个组件类型属性,用来设置分段控件的样式和所有的子视图控制器
    fileprivate var componentType: ComponentType
    {
        //分段控制的样式,有另一个方法来设置,同时设置一个数组,作为分段控件的所有子视图控制器。
        return .all(menuOptions: MenuOptions(),pagingControllers: pagingControllers)
    }
    fileprivate var pagingControllers : [UIViewController]
    {
        //初始化三个子视图控制器
        let viewController1 = ViewController1()
        let viewController2 = ViewController2()
        let viewController3 = ViewController3()
        let viewController4 = ViewController4()
        let viewController5 = ViewController5()
        return [viewController1,viewController2,viewController3,viewController4,viewController5]
    }
    //添加一个结构体属性,用来设置分段控件的外观样式
    fileprivate struct MenuOptions : MenuViewCustomizable
    {
        //设置分段控件的显示模式为分段按钮。分段控件的样式共有:标准样式、分段按钮样式和无线样式三种
        var  displayMode: MenuDisplayMode
        {

//这的模式最好设置成.standard,一般我们创建的样式通常大多都是这个
            return MenuDisplayMode.standard(widthMode: .flexible, centerItem: false, scrollingMode: .pagingEnabled)
        }
        
        //设置分段组件的焦点y模式。焦点模式共有空白、下划线、圆角三种。
        var focusMode: MenuFocusMode
        {
            return .underline(height: 12, color: UIColor.red, horizontalPadding: 0, verticalPadding: 0)
            
            //            return .roundRect(radius: 12, horizontalPadding: 8, verticalPadding: 8, selectedColor: UIColor.orange)
        }
        //设置三个分段的内容
        var itemsOptions: [MenuItemViewCustomizable]
        {
            return [MenuItem1(),MenuItem2(),MenuItem3(),MenuItem4(),MenuItem5()]
        }
    }
    //设置第一个分段的内容
    fileprivate struct MenuItem1 : MenuItemViewCustomizable
    {
        //设置第一个分段的内容
        var displayMode: MenuItemDisplayMode
        {
            return .text(title : MenuItemText(text:"News"))
        }
        
    }
    //设置第二个分段的内容
    fileprivate struct MenuItem2 : MenuItemViewCustomizable
    {
        var displayMode: MenuItemDisplayMode
        {
            return .text(title:MenuItemText.init(text: "message", color: UIColor.black, selectedColor: .white, font: UIFont.init(name: "Arial", size: 12)!, selectedFont: UIFont.init(name: "Arial", size: 16)!))
        }
        
    }
    //设置第三个分段的内容(自定义视图)
    fileprivate struct MenuItem3 : MenuItemViewCustomizable
    {
        var displayMode: MenuItemDisplayMode
        {
            let view = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 150, height: 50))
            let label = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: 150, height:50))
            label.textAlignment = .center
            label.text = "今天是个好日子"
            label.textColor = UIColor.purple
            view.addSubview(label)
            return .custom(view: view)
        }
        
    }
    //设置第二个分段的内容
    fileprivate struct MenuItem4 : MenuItemViewCustomizable
    {
        var displayMode: MenuItemDisplayMode
        {
            return .text(title:MenuItemText.init(text: "第四个数据", color: UIColor.black, selectedColor: .white, font: UIFont.init(name: "Arial", size: 12)!, selectedFont: UIFont.init(name: "Arial", size: 16)!))
        }
        
    }
    //设置第二个分段的内容
    fileprivate struct MenuItem5 : MenuItemViewCustomizable
    {
        var displayMode: MenuItemDisplayMode
        {
            return .text(title:MenuItemText.init(text: "第五个数据", color: UIColor.black, selectedColor: .white, font: UIFont.init(name: "Arial", size: 12)!, selectedFont: UIFont.init(name: "Arial", size: 16)!))
        }
        
    }
}


class ViewController: UIViewController{
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.orange
        //初始化一个分段菜单设置选项
        let options = pagInMenuOptions()
        //初始化一个分段菜单控制器
        let pagingMenuController = PagingMenuController.init(options: options)
        //设置分段菜单控制器的代理对象,为当前的视图控制器。
      
        
        //设置菜单显示器范围
        pagingMenuController.view.frame.origin.y += 20
        pagingMenuController.view.frame.size.height -= 20
        pagingMenuController.onMove = { staus in
            switch staus {
            case let .willMoveController(to: menuController, from: previousMenuController):
                print(previousMenuController)
                print(menuController)
            case let .willMoveItem(to: menuItemView, from: previousMenuItemView):
                print(menuItemView)
                print(previousMenuItemView)
            case let .didMoveItem(to: menuItemView, from: previousMenuItemView):
                print(menuItemView)
                print(previousMenuItemView)
            case let .didMoveController(to: menuController, from: previousMenuController):
                print(menuController)
                print(previousMenuController)
            case .didScrollStart:
                print("Scroll start")
            case .didScrollEnd:
                print("Scroll end")
            }
        }
        //将分段菜单控制器,作为自视图控制器,添加到当前的视图控制器
        addChild(pagingMenuController)
        //将分段菜单控制器中的视图对象,添加到根视图
        view.addSubview(pagingMenuController.view)
        //当将自视图控制器添加到俯视图控制器时,必须调用自视图控制器的移动方法
        pagingMenuController.didMove(toParent: self)

    }
    
}

下边是viewController1的类,先关设置自己设置就好

import UIKit
import PagingMenuController
class ViewController1: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let textLabel = UILabel.init(frame: self.view.frame)
        textLabel.textAlignment = .center
        textLabel.font = UIFont.systemFont(ofSize: 23)
        textLabel.text = "ViewController1"
        textLabel.backgroundColor = UIColor.red
        self.view.addSubview(textLabel)
        
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

 

你可能感兴趣的:(iOS,swift)