Latest 0.0.1
Homepage https://github.com/god-long/GLSegmentView
License MIT
Platforms ios 9.0, requires ARC
Frameworks UIKit
Authors

GLSegmentView(标题分类控件)

GLSegmentView GitHub tag license GitHub stars [GitHub forks]()

描述:

该控件一般和UIScrollView一起使用,点击控件通过代理回调给UIScrollView来
改变ContentOffset来达到控制页数的效果

功能:

  • 可根据滑动的距离来实时更新底部线条的位置和宽度;
  • 宽度是根据每个分割的控件title的宽度而定;
  • 根据滑动距离实时颜色渐变;
  • 支持codexibstoryboard;
  • 支持旋转;

展示图

iPhone

GLSegmentView

iPad

GLSegmentView

安装:

CocoaPods

pod 'GLSegmentView'

因为使用了xib文件,支持在storyboardxib中使用该控件,导致在使用时,storyboardxib找不到该控件的module
当前如果通过pod安装,并且在storyboard或者xib中使用的话,需要手动更改如图:(会有编译报错,运行没问题,待解决)

GLSegmentView

手动(当前鼓励的方式)

下载文件,添加GLSegmentView.swiftGLSegmentView.xib到工程文件中即可。

使用:

如果是iPhone需要支持旋转,必须添加如下代码

    // MARK: 如果是iPhone需要屏幕旋转功能,必须添加此方法
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        self.segmentView.beforeLayoutIndex = self.segmentView.currentSelectIndex
    }

xib or storyboard:

        let titles =  ["路飞", "Medbanks", "One", "Piece", "god~long"]

        self.segmentView.titleArray = titles
        self.segmentView.delegate = self

    //MARK: UIScrollViewDelegate
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.segmentView.updateSegmentView(scrollView.contentOffset.x, pageWidth: scrollView.frame.width)
    }

    //MARK: SegmentSlideViewDelegate
    func didSelectSegment(_ index: Int) {
        // animated必须为false,如果想点击segment的时候也动画滑动,必须添加额外的参数控制
        self.contentScrollView!.setContentOffset(CGPoint(x: CGFloat(index) * ScreenWidth, y: 0), animated: false)
    }

code:

         // 代码创建 需要去掉属性的IBOutlet
        self.segmentView = GLSegmentView(frame: CGRect(x: 0, y: 0, width: self.contentScrollView.frame.width, height: 50))
        self.segmentView.titleArray = titles
        self.segmentView?.delegate = self
        self.view.addSubview(self.segmentView!)

    //MARK: UIScrollViewDelegate
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.segmentView.updateSegmentView(scrollView.contentOffset.x, pageWidth: scrollView.frame.width)
    }

    //MARK: SegmentSlideViewDelegate
    func didSelectSegment(_ index: Int) {
        // animated必须为false,如果想点击segment的时候也动画滑动,必须添加额外的参数控制
        self.contentScrollView!.setContentOffset(CGPoint(x: CGFloat(index) * ScreenWidth, y: 0), animated: false)
    }

如有意见,欢迎issue

Latest podspec

{
    "name": "GLSegmentView",
    "version": "0.0.1",
    "summary": "A segment view for title",
    "description": "segment A segment view for title",
    "homepage": "https://github.com/god-long/GLSegmentView",
    "license": "MIT",
    "authors": {
        "god-long": "[email protected]"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/god-long/GLSegmentView.git",
        "tag": "0.0.1"
    },
    "source_files": "GLSegmentView/*.{swift}",
    "resource_bundles": {
        "GLSegmentView": [
            "GLSegmentView/*.{xib}"
        ]
    },
    "frameworks": "UIKit",
    "requires_arc": true,
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This