Skip to content

Commit

Permalink
Merge pull request #5 from iWECon/dev
Browse files Browse the repository at this point in the history
optmize api
  • Loading branch information
iWECon authored Mar 25, 2022
2 parents 1a30179 + e958bfc commit 83cae45
Show file tree
Hide file tree
Showing 25 changed files with 513 additions and 499 deletions.
198 changes: 101 additions & 97 deletions Demo/Segmenter.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

44 changes: 26 additions & 18 deletions Demo/Segmenter/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@ class ViewController: UIViewController {

let button = UIButton()

private func makeMainSegment(title: String) -> Segment {
Segment(title: title)
}
private func makeMinorSegment(title: String) -> Segment {
Segment(
title: title,
activeFont: UIFont.systemFont(ofSize: 12, weight: .medium),
inactiveFont: UIFont.systemFont(ofSize: 12, weight: .regular)
)
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

func makeButton(_ title: String, verticallyOffset: CGFloat = 0) -> Segmenter.Segment.SupplementView {
func makeButton(_ title: String, verticallyOffset: CGFloat = 0) -> Segment.SupplementView {
let btn = UIButton(type: .custom)
btn.setTitle(title, for: .normal)
btn.titleLabel?.font = .systemFont(ofSize: 12, weight: .medium)
Expand Down Expand Up @@ -48,32 +59,28 @@ class ViewController: UIViewController {
view.addSubview(segmenter)

minorSegmenter.isShadowShouldShow = false
minorSegmenter.distribution = .default
minorSegmenter.segmentConfigure = .minor
minorSegmenter.supplementaryViews = [makeButton("你好"), makeButton("来了老弟")]
minorSegmenter.distribution = .evened
minorSegmenter.segments = [
.init(title: "周杰伦,周杰伦,周杰伦"),
.init(title: "林俊杰,周杰伦"),
.init(title: "胡彦斌,周杰伦"),
makeMinorSegment(title: "周杰伦,周杰伦,周杰伦"),
makeMinorSegment(title: "林俊杰,周杰伦"),
makeMinorSegment(title: "胡彦斌,周杰伦")
]
view.addSubview(minorSegmenter)

minorCenteredSegmenter.backgroundView.backgroundColor = .clear
minorCenteredSegmenter.distribution = .centered
minorCenteredSegmenter.segmentConfigure = .main
minorCenteredSegmenter.segments = [
.init(title: "过去 Event"),
.init(title: "现在 Now"),
.init(title: "未来 Future"),
makeMainSegment(title: "过去 Event"),
makeMainSegment(title: "现在 Now"),
makeMainSegment(title: "未来 Future"),
]
view.addSubview(minorCenteredSegmenter)

aroundEvenedSegmenter.distribution = .aroundEvened
aroundEvenedSegmenter.segmentConfigure = .minor
aroundEvenedSegmenter.segments = [
.init(title: "周杰伦"),
.init(title: "林俊杰"),
.init(title: "胡彦斌")
makeMinorSegment(title: "周杰伦"),
makeMinorSegment(title: "林俊杰"),
makeMinorSegment(title: "胡彦斌")
]
view.addSubview(aroundEvenedSegmenter)

Expand Down Expand Up @@ -126,8 +133,9 @@ class ViewController: UIViewController {


extension ViewController: SegmenterSelectedDelegate {
func segmenter(_ segmenter: Segmenter, didSelect index: Int, withSegment: Segmenter.Segment, fromIndex: Int, fromSegment: Segmenter.Segment) {
// print("tapped: \(index), title: \(withSegment.title)")
}

func segmenter(_ segmenter: Segmenter, didSelect index: Int, withSegment segment: Segment, fromIndex: Int, fromSegment: Segment) {

// do something
}
}
2 changes: 0 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ let package = Package(
name: "Segmenter",
targets: ["Segmenter"]),
],
dependencies: [
],
targets: [
.target(
name: "Segmenter",
Expand Down
70 changes: 41 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,44 @@

![Preview](Demo/shot.gif)

## 用法

```swift
let segmenter = Segmenter()

// 是否需要显示阴影
segmenter.isShadowShouldShow = false
// 附加视图的间距
segmenter.supplementaryVerticallyOffset = 5

// 样式, 有 3 种: default, centered, evened, aroundEvent
// 见:+Distribution.swift -> enum Distribution: Int
segmenter.distribution = .default

// segment 点击委托
segmenter.delegate = self

// 配置视图
segmenter.segments = [
// 显示 view, activeSize 选中时的大小,inactiveSize: 未选中大小
Segment(view: UIView(), activeSize: CGSize.zero, inactiveSize: CGSize.zero),

// 显示 image
Segment(image: UIImage(), activeSize: CGSize.zero, inactiveSize: CGSize.zero)

// 显示 label, 可配置文本字体大小
Segment(title: "Label")
// 完整的 title 初始化
// Segment(title: "Label", supplementaryViews: [SupplementView], shadowHidden: false, activeFont: UIFont, activeColor: UIColor, inactiveFont: UIFont, inactiveColor: UIColor)

// 显示自定义的 View, 自定义 View 需要实现 SegmentView 协议 (UIControl + SegmentViewProvider)
// 每个 View 对应一个 SegmentInfoProvider
// 实现 SegmentInfoProvider 后, 传入 SegmentInfoProvider 协议即可
// 可参考 +Segment+Image.swift / +Segment+Label.swift / +Segment+View.swift
Segment(custom: SegmentInfoProvider)
]
```


## Demo

Expand All @@ -21,40 +59,14 @@ Download the project and then open `Demo/Segmenter.xcodeproj`
· segments 长度超出范围时可滚动显示,对 SupplementaryView 做了优化/适配

· 支持全局配置默认属性,可被实例自定义配置覆盖

```swift
/// `提供 `default` 静态属性,可配置的属性,配置后全局生效,单个实例重写对应的属性时,会覆盖全局配置`
///
/// Segmenter.default.xxxx


/// The style here is the overall default style, and the configuration information is taken from here if the Segmenter is not configured as follows after initialization
/// Even if the default style is configured, it can be configured independently in the Segmenter instance
public struct Appearance {
public var segmentSpacing: CGFloat = 15
public var contentInset = UIEdgeInsets(top: 10, left: 15, bottom: 6, right: 15)
public var animateDuration: TimeInterval = 0.34
public var shadowColor: UIColor = UIColor.black.withAlphaComponent(0.2)

/// the gradient color of supplementViewContainer
/// supplementaryContainerView 的渐变色
public var supplementarViewColors: [UIColor] = [UIColor.white, UIColor.white.withAlphaComponent(0)]

/// spacing of segment and supplementary between
/// segment 和 supplementaryView 之间的距离(最小距离)
public var spacingOfSegmentAndSupplementary: CGFloat = 20

/// Spacing between SupplementaryViews
public var supplementaryViewSpacing: CGFloat = 10
/// SupplementaryViews are shifted vertically as a whole, positive numbers down, negative numbers up
public var supplementaryVerticallyOffset: CGFloat = 0
/// SupplementaryViews are shifted horizontally as a whole, with positive numbers to the right and negative numbers to the left
public var supplementaryHorizontallyOffset: CGFloat = 0
}
// 参考
public static var `default` = Appearance()
```


## Installation

#### Swift Package Manager
`.package(url: https://github.com/iWECon/Segmenter", from: "1.0.0")`
`.package(url: https://github.com/iWECon/Segmenter", from: "2.0.0")`
2 changes: 1 addition & 1 deletion Segmenter.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = 'Segmenter'
s.version = '1.7.0'
s.version = '2.0.0'
s.license = { :type => 'MIT' }
s.homepage = 'https://github.com/iWECon/Segmenter'
s.authors = 'iWw'
Expand Down
40 changes: 0 additions & 40 deletions Sources/Segmenter/+SegmentConfigure.swift

This file was deleted.

5 changes: 2 additions & 3 deletions Sources/Segmenter/+VerticallyOffsetMapHashable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ extension Segmenter {
lhs.index == rhs.index && lhs.view == rhs.view
}


var index: Int
var view: UIView
let index: Int
let view: UIView

init(index: Int, view: UIView) {
self.index = index
Expand Down
111 changes: 0 additions & 111 deletions Sources/Segmenter/Segment/+Segment.swift

This file was deleted.

28 changes: 28 additions & 0 deletions Sources/Segmenter/Segment/Info/+ImageInfo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Created by iWw on 2021/1/21.
//

import UIKit

extension Segment {

class _ImageInfo: SegmentInfoProvider {
var viewType: (SegmentView).Type {
_Image.self
}

let activeImage: UIImage
let inactiveImage: UIImage?

let activeSize: CGSize
let inactiveSize: CGSize

init(activeImage: UIImage, inacitveImage: UIImage? = nil, activeSize: CGSize, inactiveSize: CGSize) {
self.activeImage = activeImage
self.activeSize = activeSize
self.inactiveSize = inactiveSize
self.inactiveImage = inacitveImage
}
}

}
Loading

0 comments on commit 83cae45

Please sign in to comment.