Latest 1.1.1
Homepage https://github.com/weiman152/ScrollMenusView
License MIT
Platforms ios 9.0, requires ARC
Authors

swift4 滑动菜单

swift4 scroll menus

实现思路介绍:
https://www.jianshu.com/p/3ef5575fb6f7

1.滑动菜单View,支持滑动,点击切换,不依赖任何三方,纯手工计算。

  1. Scroll menu View, support slider, click to change. Don’t rely on any sdk.

2.支持cocoa pods

  1. Support cocoa pods

platform :ios, ‘9.0’
inhibit_all_warnings!

target ‘你的项目名字’ do
use_frameworks!

pod ‘ScrollMenusView’

end

  1. 支持左图右文的菜单,初始化使用:

  2. Support menu of left image and right text.

public init(titles: [MenuModel], frame: CGRect, menuHeight: CGFloat = 44)

  1. 支持纯文字菜单,初始化使用:

  2. Also support only text menus. You can user the below func to init.

public init(titles: [String], frame: CGRect, menuHeight: CGFloat = 44)

  1. 支持动态修改某个位置上的菜单文字和图片

  2. Support Modify the menu at index .
/// 更改菜单(change menu at index)

///

/// - Parameters:

///   - menu: 要替换的菜单(the menu will be set)

///   - index: 要被替换的位置(the index will be set new menu)

public func change(menu: MenuModel, index: Int)

或者

public func change(title: String, index: Int)

使用cocoa pods示例:

The demo of user cocoa pods to support ScrollMenusView.

import UIKit
import ScrollMenusView

class ViewController: UIViewController {

private var menus: [MenuModel] = []

override func viewDidLoad() {
    super.viewDidLoad()
    setup()
}

private func setup() {
    let menu1 = MenuModel(title: "菜单一",
                          imageNormal: nil,
                          imageSelected: nil)
    let menu2 = MenuModel(title: "123",
                          imageNormal: nil,
                          imageSelected: nil)
    let menu3 = MenuModel(title: "我是好孩子",
                          imageNormal: nil,
                          imageSelected: nil)
    menus = [menu1, menu2, menu3]
    let frame = CGRect(x: 0,
                       y: 20,
                       width: view.bounds.size.width,
                       height: view.bounds.size.height - 20)
    let menu = ScrollMenus(titles: menus,
                           frame: frame,
                           menuHeight: 44)
    view.addSubview(menu)
    menu.dataSource = self
    menu.delegate = self
}

}

extension ViewController: ScrollMenusDataSource {

func menuViewViewForItems(atIndex: Int) -> UIView {
    return UIView()
}

}

extension ViewController: ScrollMenusDelegate {

func menuDidChange(currentIndex: Int) {
    print("index = (currentIndex)")
}

}

效果图:

Alt Text

不使用cocoa pods,直接导入到项目中,示例:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var menuView: UIView!
private var menus: [MenuModel] = []
private var scrollMenu: ScrollMenus?
private var childs: [UIViewController] = [FirstViewController.instance(),
                                          SecondViewController.instance(),
                                          ThirdViewController.instance()]

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

    childs.forEach { addChildViewController($0) }
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    childs.forEach { addChildViewController($0) }
}

override func viewDidLoad() {
    super.viewDidLoad()
    setup()

}

private func setup() {

    let menu1 = MenuModel(title: "购买记录",
                          imageNormal: nil,
                          imageSelected: nil)
    let menu2 = MenuModel(title: "680人",
                          imageNormal: #imageLiteral(resourceName: "personNumber_normal"),
                          imageSelected: #imageLiteral(resourceName: "personNumber_selected"))
    let menu3 = MenuModel(title: "商品详情",
                          imageNormal: nil,
                          imageSelected: nil)
    menus = [menu1, menu2, menu3]
    let menu = ScrollMenus(titles: menus,
                           frame: menuView.bounds,
                           menuHeight: 44)
    menu.dataSource = self
    menu.delegate = self
    menu.set(selected: 1)
    menu.lineColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
    menu.textColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
    menu.textSeletedColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
    menuView.addSubview(menu)
    scrollMenu = menu
}

/// 更改菜单
@IBAction func changeMenu(_ sender: Any) {
    let menu = MenuModel(title: "改变的菜单", imageNormal: nil, imageSelected: nil)
    scrollMenu?.change(menu: menu, index: 0)
}

}

extension ViewController: ScrollMenusDataSource {

func menuViewViewForItems(atIndex: Int) -> UIView {
    guard atIndex < menus.count, atIndex < childs.count else {
        return UIView()
    }
    return childs[atIndex].view
}

}

extension ViewController: ScrollMenusDelegate {

func menuDidChange(currentIndex: Int) {
    print("------------  (currentIndex)")

}

}

Latest podspec

{
    "name": "ScrollMenusView",
    "version": "1.1.1",
    "summary": "u6ed1u52a8u83dcu5355",
    "homepage": "https://github.com/weiman152/ScrollMenusView",
    "license": "MIT",
    "authors": {
        "weiman": "[email protected]"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/weiman152/ScrollMenusView.git",
        "tag": "1.1.1"
    },
    "source_files": "ScrollMenusView/**/*",
    "requires_arc": true,
    "swift_version": "4.0"
}

Pin It on Pinterest

Share This