SJFluidSegmentedControl 1.1.2

SJFluidSegmentedControl 1.1.2

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2017年3月
SwiftSwift 版本3.0
SPM支持 SPM

Sasho Jadrovski 维护。




SJFluidSegmentedControl

关于

如果您厌倦了使用默认的 UISegmentedControl,这可能会帮到您。《SJFluidSegmentedControl》是一个可定制的带有交互式过渡的分段控件,用 Swift 3.0 编写,它是基于 LUNSegmentedControl(使用 Objective-C 编写)的 LunApps

Sample

示例

要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install

需求

  • iOS 8.0+
  • Xcode 8.0+
  • Swift 3.0+

注意:SJFluidSegmentedControl 不建议从 Objective-C 使用。关于此库的 Objective-C 版本,请参阅 LUNSegmentedControl

功能

  • [x] 设置简单,使用方便
  • [x] 许多可定制选项
  • [x] 完整的文档

交流

  • 如果您发现了 错误,请打开一个问题。
  • 如果您有 功能请求,请打开一个问题。
  • 如果您想 贡献,请提交拉取请求。

使用此库的应用

如果您正在使用这个库的应用,我很愿意将其添加到这个 README 里面。请与我联系!

安装

手动安装

您还可以通过将 Classes 文件夹中的文件拖放到项目中来手动安装 SJFluidSegmentedControl

用法

使用界面构建器

  1. 将 UIView 拖放到视图中。
    • 通过设置此视图的背景颜色,您也将设置分段控制的背景颜色。
  2. 将其类更改为 SJFluidSegmentedControl。
  3. 连接其数据源。
  4. 实现唯一必需的数据源方法,该方法返回分段控制中段的数量
func numberOfSegmentsInSegmentedControl(_ segmentedControl: SJFluidSegmentedControl) -> Int

此外,您还可以通过使用属性检查器设置 cornerRadiustextColorselectedSegmentTextColorselectorViewColorapplyCornerRadiusToSelectorViewgradientBounceColorshadowShowDurationshadowHideDurationshadowsEnabled 属性。

Attributes

要自定义其他属性,请在代码中创建分段控制的 @IBOutlet 并通过代码访问它们。

使用代码

这与使用界面构建器非常相似,您只需在代码中设置自定义视图。为此有几个方法,以下是一个示例

// Conform to the data source (optionally, you can conform to the delegate)
class ViewController: UIViewController, SJFluidSegmentedControlDataSource {

    // Define a lazy var
    lazy var segmentedControl: SJFluidSegmentedControl = {
        [unowned self] in
        // Setup the frame per your needs
        let segmentedControl = SJFluidSegmentedControl(frame: CGRect(x: 0, y: 0, width: 300, height: 50))
        segmentedControl.dataSource = self
        return segmentedControl
    }()

    // Add it as a subview in viewDidLoad()
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(segmentedControl)
    }

    // Don't forget to implement the required data source method
    func numberOfSegmentsInSegmentedControl(_ segmentedControl: SJFluidSegmentedControl) -> Int {
        return 3
    }

}

数据源实现

您必须实现返回段数量的 必需 数据源方法

func numberOfSegmentsInSegmentedControl(_ segmentedControl: SJFluidSegmentedControl) -> Int

返回分段控制的段标题,并利用 NSAttributedString 功能,使用以下数据源方法自定义文本外观

@objc optional func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                                     titleForSegmentAtIndex index: Int) -> String?
@objc optional func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                                     attributedTitleForSegmentAtIndex index: Int) -> NSAttributedString?

如果需要,您可以使用以下数据源方法设置段选中状态的标题

@objc optional func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                                     titleForSelectedSegmentAtIndex index: Int) -> String?
@objc optional func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                                     attributedTitleForSelectedSegmentAtIndex index: Int) -> NSAttributedString?

您还可以使用此数据源方法设置段选中状态的标题颜色

@objc optional func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                                     titleColorForSelectedSegmentAtIndex index: Int) -> UIColor

此外,您可以使用以下数据源方法为每个段设置颜色(或颜色数组以形成渐变),以及设置左右弹跳的颜色

@objc optional func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                                     gradientColorsForSelectedSegmentAtIndex index: Int) -> [UIColor]
@objc optional func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                                     gradientColorsForBounce bounce: SJFluidSegmentedControlBounce) -> [UIColor]

如果您需要对每个段进行更复杂的布局,您可以使用以下数据源方法返回一个自定义视图

@objc optional func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                                     viewForSegmentAtIndex index: Int) -> UIView
@objc optional func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                                     viewForSelectedSegmentAtIndex index: Int) -> UIView

代理实现

代理方法提供了一些常用事件的回调,例如

@objc optional func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                                     didChangeFromSegmentAtIndex fromIndex: Int,
                                     toSegmentAtIndex toIndex:Int)
@objc optional func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                                     willChangeFromSegment fromSegment: Int)
@objc optional func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                                     didScrollWithXOffset offset: CGFloat)

此外,如果您需要控制段之间的过渡,可以使用以下代理方法

@objc optional func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                                     setupSegmentAtIndex segmentIndex: Int,
                                     unselectedView unselectedSegmentView: UIView,
                                     selectedView selectedSegmentView: UIView,
                                     withSelectionPercent percent: CGFloat)
@objc optional func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                                     resetSegmentAtIndex segmentIndex: Int,
                                     unselectedView unselectedSegmentView: UIView,
                                     selectedView selectedSegmentView: UIView)

附加属性

其他自定义选项可通过以下公开属性获得

// The index of the currently selected segment. It ranges from 0 to segmentsCount-1. Default is `0`.
open var currentSegment: Int

// The number of segments in the segmented control. Default is `1`.
fileprivate(set) public var segmentsCount: Int

// The transition style between the default and selected state of the segments. Default is `.fade`.
open var transitionStyle: SJFluidSegmentedControlTransitionStyle

// The style of the selecton shape. Default is `.liquid`.
open var shapeStyle: SJFluidSegmentedControlShapeStyle

// The corner radius of the segmented control. Default is `0.0`.
@IBInspectable open var cornerRadius: CGFloat

// The color of the text for the default state of a segment. Default is `.black`. This property will be overriden if the delegate for attributed titles/views is implemented.
@IBInspectable open var textColor: UIColor

// The color of the text for the selected state of a segment. Default is `.white`. This property will be overriden if the delegate for attributed titles for selected state or views for selected state is implemented.
@IBInspectable open var selectedSegmentTextColor: UIColor

// The text font for the titles of the segmented control in both states if the data source does not provide attributed titles or views. Default is `.systemFont(ofSize: 14)`.
open var textFont: UIFont

// The color of the selector. Default is `.clear`. **Note:** If set, it is overlayed over the gradient colors.
@IBInspectable open var selectorViewColor: UIColor

// A boolean value to determine whether the selector should have rounded corners. Default is `false`.
@IBInspectable open var applyCornerRadiusToSelectorView: Bool

// The color for the bounce if the data source does not provide colors for bounces. Default is `.red`.
@IBInspectable open var gradientBounceColor: UIColor

// The duration of the show shadow animation. Default is `0.5`.
@IBInspectable open var shadowShowDuration: CGFloat

// The duration of the hide shadow animation. Default is `0.8`.
@IBInspectable open var shadowHideDuration: CGFloat

// A boolean value to determine whether shadows should be applied. Default is `true`.
@IBInspectable open var shadowsEnabled: Bool

重新加载数据

如果您需要重新加载数分控制的数据,只需调用

segmentedControl.reloadData()

作者

此库已被 Sasho Jadrovski 适配为 Swift 3.0+,请访问 http://jadrovski.com。正如上文所述,原始创建者是 LunApps

许可

SJFluidSegmentedControl 可在 MIT 许可下获得。有关更多信息,请参阅 LICENSE 文件。