├── .gitignore
├── .swift-version
├── Example
├── AppDelegate.swift
├── Assets.xcassets
│ └── AppIcon.appiconset
│ │ └── Contents.json
├── Base.lproj
│ ├── LaunchScreen.storyboard
│ └── Main.storyboard
├── Info.plist
└── ViewController.swift
├── LICENSE
├── README.md
├── VGSegment.podspec
├── VGSegment.xcodeproj
├── project.pbxproj
├── project.xcworkspace
│ └── contents.xcworkspacedata
└── xcshareddata
│ └── xcschemes
│ └── VGSegment.xcscheme
├── VGSegment
├── Info.plist
├── VGSegment+Animation.swift
├── VGSegment+UITapGestureRecognizer.swift
├── VGSegment.h
├── VGSegment.swift
└── VGSegmentConfiguration.swift
├── demo1.gif
└── demo2.gif
/.gitignore:
--------------------------------------------------------------------------------
1 | # Xcode
2 | #
3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4 |
5 | ## Build generated
6 | build/
7 | DerivedData/
8 |
9 | ## Various settings
10 | *.pbxuser
11 | !default.pbxuser
12 | *.mode1v3
13 | !default.mode1v3
14 | *.mode2v3
15 | !default.mode2v3
16 | *.perspectivev3
17 | !default.perspectivev3
18 | xcuserdata/
19 |
20 | ## Other
21 | *.moved-aside
22 | *.xccheckout
23 | *.xcscmblueprint
24 |
25 | ## Obj-C/Swift specific
26 | *.hmap
27 | *.ipa
28 | *.dSYM.zip
29 | *.dSYM
30 |
31 | ## Playgrounds
32 | timeline.xctimeline
33 | playground.xcworkspace
34 |
35 | # Swift Package Manager
36 | #
37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
38 | # Packages/
39 | # Package.pins
40 | .build/
41 |
42 | # CocoaPods
43 | #
44 | # We recommend against adding the Pods directory to your .gitignore. However
45 | # you should judge for yourself, the pros and cons are mentioned at:
46 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
47 | #
48 | # Pods/
49 |
50 | # Carthage
51 | #
52 | # Add this line if you want to avoid checking in source code from Carthage dependencies.
53 | # Carthage/Checkouts
54 |
55 | Carthage/Build
56 |
57 | # fastlane
58 | #
59 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
60 | # screenshots whenever they are needed.
61 | # For more information about the recommended setup visit:
62 | # https://docs.fastlane.tools/best-practices/source-control/#source-control
63 |
64 | fastlane/report.xml
65 | fastlane/Preview.html
66 | fastlane/screenshots
67 | fastlane/test_output
68 |
--------------------------------------------------------------------------------
/.swift-version:
--------------------------------------------------------------------------------
1 | 5.0
2 |
--------------------------------------------------------------------------------
/Example/AppDelegate.swift:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.swift
3 | // Example
4 | //
5 | // Created by Vein on 2017/5/26.
6 | // Copyright © 2017年 Vein. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | @UIApplicationMain
12 | class AppDelegate: UIResponder, UIApplicationDelegate {
13 |
14 | var window: UIWindow?
15 |
16 |
17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
18 | // Override point for customization after application launch.
19 | return true
20 | }
21 |
22 | func applicationWillResignActive(_ application: UIApplication) {
23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
25 | }
26 |
27 | func applicationDidEnterBackground(_ application: UIApplication) {
28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
30 | }
31 |
32 | func applicationWillEnterForeground(_ application: UIApplication) {
33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
34 | }
35 |
36 | func applicationDidBecomeActive(_ application: UIApplication) {
37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
38 | }
39 |
40 | func applicationWillTerminate(_ application: UIApplication) {
41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
42 | }
43 |
44 |
45 | }
46 |
47 |
--------------------------------------------------------------------------------
/Example/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ios-marketing",
45 | "size" : "1024x1024",
46 | "scale" : "1x"
47 | }
48 | ],
49 | "info" : {
50 | "version" : 1,
51 | "author" : "xcode"
52 | }
53 | }
--------------------------------------------------------------------------------
/Example/Base.lproj/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/Example/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/Example/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationPortrait
34 | UIInterfaceOrientationLandscapeLeft
35 | UIInterfaceOrientationLandscapeRight
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/Example/ViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.swift
3 | // Example
4 | //
5 | // Created by Vein on 2017/5/26.
6 | // Copyright © 2017年 Vein. All rights reserved.
7 | //
8 |
9 | import UIKit
10 | import VGSegment
11 |
12 | class ViewController: UIViewController, VGSegmentDelegate{
13 |
14 | override func viewDidLoad() {
15 | super.viewDidLoad()
16 |
17 | let rect = CGRect(x: 0, y: 250, width: view.frame.width, height: 45)
18 | let titles = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"]
19 |
20 | let segment = VGSegment(frame: rect, titles: titles)
21 | segment.delegate = self
22 | view.addSubview(segment)
23 |
24 | var configuration: VGSegmentConfiguration {
25 | let configura = VGSegmentConfiguration()
26 | // TODO: configuration segment
27 | return configura
28 | }
29 |
30 | segment.configuration = configuration
31 | }
32 |
33 | func didSelectAtIndex(_ index: Int) {
34 | print("selectIndex:", index)
35 | }
36 |
37 | }
38 |
39 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 WenRong
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # VGSegment
2 |
3 | A segment menu with line animation
4 |
5 |
6 | 
7 | [](http://cocoadocs.org/docsets/VGSegment/1.0/)
8 | [](https://github.com/VeinGuo/VGSegment/blob/master/LICENSE)
9 | [](https://github.com/VeinGuo/VGSegment)
10 | [](http://cocoadocs.org/docsets/VGSegment/1.0.2/)
11 |
12 | 
13 |
14 | 
15 |
16 | ## Requirements
17 |
18 | - Swift 5
19 | - iOS 8.0+
20 | - XCode 10.2
21 |
22 | ## Usage
23 |
24 | ```swift
25 | class ViewController: UIViewController, VGSegmentDelegate{
26 |
27 | override func viewDidLoad() {
28 | super.viewDidLoad()
29 |
30 | let rect = CGRect(x: 0, y: 250, width: view.frame.width, height: 45)
31 | let titles = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"]
32 |
33 | let segment = VGSegment(frame: rect, titles: titles)
34 | segment.delegate = self
35 | view.addSubview(segment)
36 |
37 | var configuration: VGSegmentConfiguration {
38 | let configura = VGSegmentConfiguration()
39 | // TODO: configuration segment
40 | return configura
41 | }
42 |
43 | segment.configuration = configuration
44 | }
45 |
46 | func didSelectAtIndex(_ index: Int) {
47 | print("selectIndex:", index)
48 | }
49 |
50 | }
51 | ```
52 |
53 | ## Installation
54 | - Download VGSegment. Move to your project.
55 |
56 | - Cocoapods
57 |
58 | ```
59 | platform :ios, ‘8.0’
60 | use_frameworks!
61 | pod "VGSegment"
62 | ```
63 |
64 | - Carthage
65 |
66 | ```
67 | github "VeinGuo/VGSegment" ~> 1.0.1
68 | ```
69 |
70 | ## Version
71 | - 1.0 Release (05/28/2017)
72 | - 1.0.1 Release (05/31/2017)
73 | - 1.0.2 fix horizontal and vertical autolayout
74 |
75 |
--------------------------------------------------------------------------------
/VGSegment.podspec:
--------------------------------------------------------------------------------
1 | Pod::Spec.new do |s|
2 |
3 | # Guide: https://github.com/VeinGuo/VGSegment
4 |
5 | s.name = "VGSegment"
6 | s.version = "1.0.4"
7 | s.summary = "A segment menu with line animation"
8 | s.license = { :type => 'MIT License', :file => 'LICENSE' }
9 | s.homepage = "https://github.com/VeinGuo/VGSegment"
10 | s.author = { "VeinGuo" => "https://github.com/VeinGuo" }
11 | s.ios.deployment_target = "8.0"
12 | s.platform = :ios, "8.0"
13 | s.source = { :git => "https://github.com/VeinGuo/VGSegment.git", :tag => s.version }
14 | s.source_files = 'VGSegment/**/*.{h,m,swift}'
15 | s.requires_arc = true
16 |
17 | end
18 |
19 |
--------------------------------------------------------------------------------
/VGSegment.xcodeproj/project.pbxproj:
--------------------------------------------------------------------------------
1 | // !$*UTF8*$!
2 | {
3 | archiveVersion = 1;
4 | classes = {
5 | };
6 | objectVersion = 46;
7 | objects = {
8 |
9 | /* Begin PBXBuildFile section */
10 | 333C3D481ED7C24400BAB307 /* VGSegment.h in Headers */ = {isa = PBXBuildFile; fileRef = 333C3D461ED7C24400BAB307 /* VGSegment.h */; settings = {ATTRIBUTES = (Public, ); }; };
11 | 333C3D551ED7C2A700BAB307 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 333C3D541ED7C2A700BAB307 /* AppDelegate.swift */; };
12 | 333C3D571ED7C2A700BAB307 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 333C3D561ED7C2A700BAB307 /* ViewController.swift */; };
13 | 333C3D5A1ED7C2A700BAB307 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 333C3D581ED7C2A700BAB307 /* Main.storyboard */; };
14 | 333C3D5C1ED7C2A700BAB307 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 333C3D5B1ED7C2A700BAB307 /* Assets.xcassets */; };
15 | 333C3D5F1ED7C2A700BAB307 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 333C3D5D1ED7C2A700BAB307 /* LaunchScreen.storyboard */; };
16 | 333C3D641ED7C2B800BAB307 /* VGSegment.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 333C3D431ED7C24400BAB307 /* VGSegment.framework */; };
17 | 333C3D651ED7C2B800BAB307 /* VGSegment.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 333C3D431ED7C24400BAB307 /* VGSegment.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
18 | 333C3D6C1ED7C33D00BAB307 /* VGSegment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 333C3D6B1ED7C33D00BAB307 /* VGSegment.swift */; };
19 | 333C3D701ED7D27A00BAB307 /* VGSegmentConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 333C3D6F1ED7D27A00BAB307 /* VGSegmentConfiguration.swift */; };
20 | 8024F3BB1ED9CAF8000E8CD0 /* VGSegment+UITapGestureRecognizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8024F3BA1ED9CAF8000E8CD0 /* VGSegment+UITapGestureRecognizer.swift */; };
21 | 8065362C1EDAD5050057B2B1 /* VGSegment+Animation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8065362B1EDAD5050057B2B1 /* VGSegment+Animation.swift */; };
22 | /* End PBXBuildFile section */
23 |
24 | /* Begin PBXContainerItemProxy section */
25 | 333C3D661ED7C2B800BAB307 /* PBXContainerItemProxy */ = {
26 | isa = PBXContainerItemProxy;
27 | containerPortal = 333C3D3A1ED7C24400BAB307 /* Project object */;
28 | proxyType = 1;
29 | remoteGlobalIDString = 333C3D421ED7C24400BAB307;
30 | remoteInfo = VGSegment;
31 | };
32 | /* End PBXContainerItemProxy section */
33 |
34 | /* Begin PBXCopyFilesBuildPhase section */
35 | 333C3D681ED7C2B800BAB307 /* Embed Frameworks */ = {
36 | isa = PBXCopyFilesBuildPhase;
37 | buildActionMask = 2147483647;
38 | dstPath = "";
39 | dstSubfolderSpec = 10;
40 | files = (
41 | 333C3D651ED7C2B800BAB307 /* VGSegment.framework in Embed Frameworks */,
42 | );
43 | name = "Embed Frameworks";
44 | runOnlyForDeploymentPostprocessing = 0;
45 | };
46 | /* End PBXCopyFilesBuildPhase section */
47 |
48 | /* Begin PBXFileReference section */
49 | 333C3D431ED7C24400BAB307 /* VGSegment.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = VGSegment.framework; sourceTree = BUILT_PRODUCTS_DIR; };
50 | 333C3D461ED7C24400BAB307 /* VGSegment.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VGSegment.h; sourceTree = ""; };
51 | 333C3D471ED7C24400BAB307 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
52 | 333C3D521ED7C2A700BAB307 /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
53 | 333C3D541ED7C2A700BAB307 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; };
54 | 333C3D561ED7C2A700BAB307 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; };
55 | 333C3D591ED7C2A700BAB307 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; };
56 | 333C3D5B1ED7C2A700BAB307 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; };
57 | 333C3D5E1ED7C2A700BAB307 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; };
58 | 333C3D601ED7C2A700BAB307 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
59 | 333C3D6B1ED7C33D00BAB307 /* VGSegment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VGSegment.swift; sourceTree = ""; };
60 | 333C3D6F1ED7D27A00BAB307 /* VGSegmentConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VGSegmentConfiguration.swift; sourceTree = ""; };
61 | 8024F3BA1ED9CAF8000E8CD0 /* VGSegment+UITapGestureRecognizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "VGSegment+UITapGestureRecognizer.swift"; sourceTree = ""; };
62 | 8065362B1EDAD5050057B2B1 /* VGSegment+Animation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "VGSegment+Animation.swift"; sourceTree = ""; };
63 | /* End PBXFileReference section */
64 |
65 | /* Begin PBXFrameworksBuildPhase section */
66 | 333C3D3F1ED7C24400BAB307 /* Frameworks */ = {
67 | isa = PBXFrameworksBuildPhase;
68 | buildActionMask = 2147483647;
69 | files = (
70 | );
71 | runOnlyForDeploymentPostprocessing = 0;
72 | };
73 | 333C3D4F1ED7C2A700BAB307 /* Frameworks */ = {
74 | isa = PBXFrameworksBuildPhase;
75 | buildActionMask = 2147483647;
76 | files = (
77 | 333C3D641ED7C2B800BAB307 /* VGSegment.framework in Frameworks */,
78 | );
79 | runOnlyForDeploymentPostprocessing = 0;
80 | };
81 | /* End PBXFrameworksBuildPhase section */
82 |
83 | /* Begin PBXGroup section */
84 | 333C3D391ED7C24400BAB307 = {
85 | isa = PBXGroup;
86 | children = (
87 | 333C3D531ED7C2A700BAB307 /* Example */,
88 | 333C3D451ED7C24400BAB307 /* VGSegment */,
89 | 333C3D441ED7C24400BAB307 /* Products */,
90 | );
91 | sourceTree = "";
92 | };
93 | 333C3D441ED7C24400BAB307 /* Products */ = {
94 | isa = PBXGroup;
95 | children = (
96 | 333C3D431ED7C24400BAB307 /* VGSegment.framework */,
97 | 333C3D521ED7C2A700BAB307 /* Example.app */,
98 | );
99 | name = Products;
100 | sourceTree = "";
101 | };
102 | 333C3D451ED7C24400BAB307 /* VGSegment */ = {
103 | isa = PBXGroup;
104 | children = (
105 | 333C3D461ED7C24400BAB307 /* VGSegment.h */,
106 | 333C3D471ED7C24400BAB307 /* Info.plist */,
107 | 333C3D6B1ED7C33D00BAB307 /* VGSegment.swift */,
108 | 333C3D6F1ED7D27A00BAB307 /* VGSegmentConfiguration.swift */,
109 | 8024F3BA1ED9CAF8000E8CD0 /* VGSegment+UITapGestureRecognizer.swift */,
110 | 8065362B1EDAD5050057B2B1 /* VGSegment+Animation.swift */,
111 | );
112 | path = VGSegment;
113 | sourceTree = "";
114 | };
115 | 333C3D531ED7C2A700BAB307 /* Example */ = {
116 | isa = PBXGroup;
117 | children = (
118 | 333C3D541ED7C2A700BAB307 /* AppDelegate.swift */,
119 | 333C3D561ED7C2A700BAB307 /* ViewController.swift */,
120 | 333C3D581ED7C2A700BAB307 /* Main.storyboard */,
121 | 333C3D5B1ED7C2A700BAB307 /* Assets.xcassets */,
122 | 333C3D5D1ED7C2A700BAB307 /* LaunchScreen.storyboard */,
123 | 333C3D601ED7C2A700BAB307 /* Info.plist */,
124 | );
125 | path = Example;
126 | sourceTree = "";
127 | };
128 | /* End PBXGroup section */
129 |
130 | /* Begin PBXHeadersBuildPhase section */
131 | 333C3D401ED7C24400BAB307 /* Headers */ = {
132 | isa = PBXHeadersBuildPhase;
133 | buildActionMask = 2147483647;
134 | files = (
135 | 333C3D481ED7C24400BAB307 /* VGSegment.h in Headers */,
136 | );
137 | runOnlyForDeploymentPostprocessing = 0;
138 | };
139 | /* End PBXHeadersBuildPhase section */
140 |
141 | /* Begin PBXNativeTarget section */
142 | 333C3D421ED7C24400BAB307 /* VGSegment */ = {
143 | isa = PBXNativeTarget;
144 | buildConfigurationList = 333C3D4B1ED7C24400BAB307 /* Build configuration list for PBXNativeTarget "VGSegment" */;
145 | buildPhases = (
146 | 333C3D3E1ED7C24400BAB307 /* Sources */,
147 | 333C3D3F1ED7C24400BAB307 /* Frameworks */,
148 | 333C3D401ED7C24400BAB307 /* Headers */,
149 | 333C3D411ED7C24400BAB307 /* Resources */,
150 | );
151 | buildRules = (
152 | );
153 | dependencies = (
154 | );
155 | name = VGSegment;
156 | productName = VGSegment;
157 | productReference = 333C3D431ED7C24400BAB307 /* VGSegment.framework */;
158 | productType = "com.apple.product-type.framework";
159 | };
160 | 333C3D511ED7C2A700BAB307 /* Example */ = {
161 | isa = PBXNativeTarget;
162 | buildConfigurationList = 333C3D611ED7C2A700BAB307 /* Build configuration list for PBXNativeTarget "Example" */;
163 | buildPhases = (
164 | 333C3D4E1ED7C2A700BAB307 /* Sources */,
165 | 333C3D4F1ED7C2A700BAB307 /* Frameworks */,
166 | 333C3D501ED7C2A700BAB307 /* Resources */,
167 | 333C3D681ED7C2B800BAB307 /* Embed Frameworks */,
168 | );
169 | buildRules = (
170 | );
171 | dependencies = (
172 | 333C3D671ED7C2B800BAB307 /* PBXTargetDependency */,
173 | );
174 | name = Example;
175 | productName = Example;
176 | productReference = 333C3D521ED7C2A700BAB307 /* Example.app */;
177 | productType = "com.apple.product-type.application";
178 | };
179 | /* End PBXNativeTarget section */
180 |
181 | /* Begin PBXProject section */
182 | 333C3D3A1ED7C24400BAB307 /* Project object */ = {
183 | isa = PBXProject;
184 | attributes = {
185 | LastSwiftUpdateCheck = 0820;
186 | LastUpgradeCheck = 0900;
187 | ORGANIZATIONNAME = Vein;
188 | TargetAttributes = {
189 | 333C3D421ED7C24400BAB307 = {
190 | CreatedOnToolsVersion = 8.2.1;
191 | DevelopmentTeam = TVRG578TKB;
192 | LastSwiftMigration = 1020;
193 | ProvisioningStyle = Automatic;
194 | };
195 | 333C3D511ED7C2A700BAB307 = {
196 | CreatedOnToolsVersion = 8.2.1;
197 | DevelopmentTeam = TVRG578TKB;
198 | LastSwiftMigration = 1020;
199 | ProvisioningStyle = Automatic;
200 | };
201 | };
202 | };
203 | buildConfigurationList = 333C3D3D1ED7C24400BAB307 /* Build configuration list for PBXProject "VGSegment" */;
204 | compatibilityVersion = "Xcode 3.2";
205 | developmentRegion = English;
206 | hasScannedForEncodings = 0;
207 | knownRegions = (
208 | English,
209 | en,
210 | Base,
211 | );
212 | mainGroup = 333C3D391ED7C24400BAB307;
213 | productRefGroup = 333C3D441ED7C24400BAB307 /* Products */;
214 | projectDirPath = "";
215 | projectRoot = "";
216 | targets = (
217 | 333C3D421ED7C24400BAB307 /* VGSegment */,
218 | 333C3D511ED7C2A700BAB307 /* Example */,
219 | );
220 | };
221 | /* End PBXProject section */
222 |
223 | /* Begin PBXResourcesBuildPhase section */
224 | 333C3D411ED7C24400BAB307 /* Resources */ = {
225 | isa = PBXResourcesBuildPhase;
226 | buildActionMask = 2147483647;
227 | files = (
228 | );
229 | runOnlyForDeploymentPostprocessing = 0;
230 | };
231 | 333C3D501ED7C2A700BAB307 /* Resources */ = {
232 | isa = PBXResourcesBuildPhase;
233 | buildActionMask = 2147483647;
234 | files = (
235 | 333C3D5F1ED7C2A700BAB307 /* LaunchScreen.storyboard in Resources */,
236 | 333C3D5C1ED7C2A700BAB307 /* Assets.xcassets in Resources */,
237 | 333C3D5A1ED7C2A700BAB307 /* Main.storyboard in Resources */,
238 | );
239 | runOnlyForDeploymentPostprocessing = 0;
240 | };
241 | /* End PBXResourcesBuildPhase section */
242 |
243 | /* Begin PBXSourcesBuildPhase section */
244 | 333C3D3E1ED7C24400BAB307 /* Sources */ = {
245 | isa = PBXSourcesBuildPhase;
246 | buildActionMask = 2147483647;
247 | files = (
248 | 333C3D6C1ED7C33D00BAB307 /* VGSegment.swift in Sources */,
249 | 8024F3BB1ED9CAF8000E8CD0 /* VGSegment+UITapGestureRecognizer.swift in Sources */,
250 | 333C3D701ED7D27A00BAB307 /* VGSegmentConfiguration.swift in Sources */,
251 | 8065362C1EDAD5050057B2B1 /* VGSegment+Animation.swift in Sources */,
252 | );
253 | runOnlyForDeploymentPostprocessing = 0;
254 | };
255 | 333C3D4E1ED7C2A700BAB307 /* Sources */ = {
256 | isa = PBXSourcesBuildPhase;
257 | buildActionMask = 2147483647;
258 | files = (
259 | 333C3D571ED7C2A700BAB307 /* ViewController.swift in Sources */,
260 | 333C3D551ED7C2A700BAB307 /* AppDelegate.swift in Sources */,
261 | );
262 | runOnlyForDeploymentPostprocessing = 0;
263 | };
264 | /* End PBXSourcesBuildPhase section */
265 |
266 | /* Begin PBXTargetDependency section */
267 | 333C3D671ED7C2B800BAB307 /* PBXTargetDependency */ = {
268 | isa = PBXTargetDependency;
269 | target = 333C3D421ED7C24400BAB307 /* VGSegment */;
270 | targetProxy = 333C3D661ED7C2B800BAB307 /* PBXContainerItemProxy */;
271 | };
272 | /* End PBXTargetDependency section */
273 |
274 | /* Begin PBXVariantGroup section */
275 | 333C3D581ED7C2A700BAB307 /* Main.storyboard */ = {
276 | isa = PBXVariantGroup;
277 | children = (
278 | 333C3D591ED7C2A700BAB307 /* Base */,
279 | );
280 | name = Main.storyboard;
281 | sourceTree = "";
282 | };
283 | 333C3D5D1ED7C2A700BAB307 /* LaunchScreen.storyboard */ = {
284 | isa = PBXVariantGroup;
285 | children = (
286 | 333C3D5E1ED7C2A700BAB307 /* Base */,
287 | );
288 | name = LaunchScreen.storyboard;
289 | sourceTree = "";
290 | };
291 | /* End PBXVariantGroup section */
292 |
293 | /* Begin XCBuildConfiguration section */
294 | 333C3D491ED7C24400BAB307 /* Debug */ = {
295 | isa = XCBuildConfiguration;
296 | buildSettings = {
297 | ALWAYS_SEARCH_USER_PATHS = NO;
298 | CLANG_ANALYZER_NONNULL = YES;
299 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
300 | CLANG_CXX_LIBRARY = "libc++";
301 | CLANG_ENABLE_MODULES = YES;
302 | CLANG_ENABLE_OBJC_ARC = YES;
303 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
304 | CLANG_WARN_BOOL_CONVERSION = YES;
305 | CLANG_WARN_COMMA = YES;
306 | CLANG_WARN_CONSTANT_CONVERSION = YES;
307 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
308 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
309 | CLANG_WARN_EMPTY_BODY = YES;
310 | CLANG_WARN_ENUM_CONVERSION = YES;
311 | CLANG_WARN_INFINITE_RECURSION = YES;
312 | CLANG_WARN_INT_CONVERSION = YES;
313 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
314 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
315 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
316 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
317 | CLANG_WARN_STRICT_PROTOTYPES = YES;
318 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
319 | CLANG_WARN_UNREACHABLE_CODE = YES;
320 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
321 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
322 | COPY_PHASE_STRIP = NO;
323 | CURRENT_PROJECT_VERSION = 1;
324 | DEBUG_INFORMATION_FORMAT = dwarf;
325 | ENABLE_STRICT_OBJC_MSGSEND = YES;
326 | ENABLE_TESTABILITY = YES;
327 | GCC_C_LANGUAGE_STANDARD = gnu99;
328 | GCC_DYNAMIC_NO_PIC = NO;
329 | GCC_NO_COMMON_BLOCKS = YES;
330 | GCC_OPTIMIZATION_LEVEL = 0;
331 | GCC_PREPROCESSOR_DEFINITIONS = (
332 | "DEBUG=1",
333 | "$(inherited)",
334 | );
335 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
336 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
337 | GCC_WARN_UNDECLARED_SELECTOR = YES;
338 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
339 | GCC_WARN_UNUSED_FUNCTION = YES;
340 | GCC_WARN_UNUSED_VARIABLE = YES;
341 | IPHONEOS_DEPLOYMENT_TARGET = 10.2;
342 | MTL_ENABLE_DEBUG_INFO = YES;
343 | ONLY_ACTIVE_ARCH = YES;
344 | SDKROOT = iphoneos;
345 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
346 | SWIFT_OPTIMIZATION_LEVEL = "-Onone";
347 | TARGETED_DEVICE_FAMILY = "1,2";
348 | VERSIONING_SYSTEM = "apple-generic";
349 | VERSION_INFO_PREFIX = "";
350 | };
351 | name = Debug;
352 | };
353 | 333C3D4A1ED7C24400BAB307 /* Release */ = {
354 | isa = XCBuildConfiguration;
355 | buildSettings = {
356 | ALWAYS_SEARCH_USER_PATHS = NO;
357 | CLANG_ANALYZER_NONNULL = YES;
358 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
359 | CLANG_CXX_LIBRARY = "libc++";
360 | CLANG_ENABLE_MODULES = YES;
361 | CLANG_ENABLE_OBJC_ARC = YES;
362 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
363 | CLANG_WARN_BOOL_CONVERSION = YES;
364 | CLANG_WARN_COMMA = YES;
365 | CLANG_WARN_CONSTANT_CONVERSION = YES;
366 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
367 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
368 | CLANG_WARN_EMPTY_BODY = YES;
369 | CLANG_WARN_ENUM_CONVERSION = YES;
370 | CLANG_WARN_INFINITE_RECURSION = YES;
371 | CLANG_WARN_INT_CONVERSION = YES;
372 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
373 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
374 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
375 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
376 | CLANG_WARN_STRICT_PROTOTYPES = YES;
377 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
378 | CLANG_WARN_UNREACHABLE_CODE = YES;
379 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
380 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
381 | COPY_PHASE_STRIP = NO;
382 | CURRENT_PROJECT_VERSION = 1;
383 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
384 | ENABLE_NS_ASSERTIONS = NO;
385 | ENABLE_STRICT_OBJC_MSGSEND = YES;
386 | GCC_C_LANGUAGE_STANDARD = gnu99;
387 | GCC_NO_COMMON_BLOCKS = YES;
388 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
389 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
390 | GCC_WARN_UNDECLARED_SELECTOR = YES;
391 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
392 | GCC_WARN_UNUSED_FUNCTION = YES;
393 | GCC_WARN_UNUSED_VARIABLE = YES;
394 | IPHONEOS_DEPLOYMENT_TARGET = 10.2;
395 | MTL_ENABLE_DEBUG_INFO = NO;
396 | SDKROOT = iphoneos;
397 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
398 | TARGETED_DEVICE_FAMILY = "1,2";
399 | VALIDATE_PRODUCT = YES;
400 | VERSIONING_SYSTEM = "apple-generic";
401 | VERSION_INFO_PREFIX = "";
402 | };
403 | name = Release;
404 | };
405 | 333C3D4C1ED7C24400BAB307 /* Debug */ = {
406 | isa = XCBuildConfiguration;
407 | buildSettings = {
408 | CLANG_ENABLE_MODULES = YES;
409 | CODE_SIGN_IDENTITY = "";
410 | DEFINES_MODULE = YES;
411 | DEVELOPMENT_TEAM = TVRG578TKB;
412 | DYLIB_COMPATIBILITY_VERSION = 1;
413 | DYLIB_CURRENT_VERSION = 1;
414 | DYLIB_INSTALL_NAME_BASE = "@rpath";
415 | INFOPLIST_FILE = VGSegment/Info.plist;
416 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
417 | IPHONEOS_DEPLOYMENT_TARGET = 8.0;
418 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
419 | PRODUCT_BUNDLE_IDENTIFIER = com.vein.VGSegment;
420 | PRODUCT_NAME = "$(TARGET_NAME)";
421 | SKIP_INSTALL = YES;
422 | SWIFT_OPTIMIZATION_LEVEL = "-Onone";
423 | SWIFT_VERSION = 5.0;
424 | TARGETED_DEVICE_FAMILY = 1;
425 | };
426 | name = Debug;
427 | };
428 | 333C3D4D1ED7C24400BAB307 /* Release */ = {
429 | isa = XCBuildConfiguration;
430 | buildSettings = {
431 | CLANG_ENABLE_MODULES = YES;
432 | CODE_SIGN_IDENTITY = "";
433 | DEFINES_MODULE = YES;
434 | DEVELOPMENT_TEAM = TVRG578TKB;
435 | DYLIB_COMPATIBILITY_VERSION = 1;
436 | DYLIB_CURRENT_VERSION = 1;
437 | DYLIB_INSTALL_NAME_BASE = "@rpath";
438 | INFOPLIST_FILE = VGSegment/Info.plist;
439 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
440 | IPHONEOS_DEPLOYMENT_TARGET = 8.0;
441 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
442 | PRODUCT_BUNDLE_IDENTIFIER = com.vein.VGSegment;
443 | PRODUCT_NAME = "$(TARGET_NAME)";
444 | SKIP_INSTALL = YES;
445 | SWIFT_VERSION = 5.0;
446 | TARGETED_DEVICE_FAMILY = 1;
447 | };
448 | name = Release;
449 | };
450 | 333C3D621ED7C2A700BAB307 /* Debug */ = {
451 | isa = XCBuildConfiguration;
452 | buildSettings = {
453 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
454 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
455 | DEVELOPMENT_TEAM = TVRG578TKB;
456 | INFOPLIST_FILE = Example/Info.plist;
457 | IPHONEOS_DEPLOYMENT_TARGET = 8.0;
458 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
459 | PRODUCT_BUNDLE_IDENTIFIER = com.vein.Example;
460 | PRODUCT_NAME = "$(TARGET_NAME)";
461 | SWIFT_VERSION = 5.0;
462 | };
463 | name = Debug;
464 | };
465 | 333C3D631ED7C2A700BAB307 /* Release */ = {
466 | isa = XCBuildConfiguration;
467 | buildSettings = {
468 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
469 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
470 | DEVELOPMENT_TEAM = TVRG578TKB;
471 | INFOPLIST_FILE = Example/Info.plist;
472 | IPHONEOS_DEPLOYMENT_TARGET = 8.0;
473 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
474 | PRODUCT_BUNDLE_IDENTIFIER = com.vein.Example;
475 | PRODUCT_NAME = "$(TARGET_NAME)";
476 | SWIFT_VERSION = 5.0;
477 | };
478 | name = Release;
479 | };
480 | /* End XCBuildConfiguration section */
481 |
482 | /* Begin XCConfigurationList section */
483 | 333C3D3D1ED7C24400BAB307 /* Build configuration list for PBXProject "VGSegment" */ = {
484 | isa = XCConfigurationList;
485 | buildConfigurations = (
486 | 333C3D491ED7C24400BAB307 /* Debug */,
487 | 333C3D4A1ED7C24400BAB307 /* Release */,
488 | );
489 | defaultConfigurationIsVisible = 0;
490 | defaultConfigurationName = Release;
491 | };
492 | 333C3D4B1ED7C24400BAB307 /* Build configuration list for PBXNativeTarget "VGSegment" */ = {
493 | isa = XCConfigurationList;
494 | buildConfigurations = (
495 | 333C3D4C1ED7C24400BAB307 /* Debug */,
496 | 333C3D4D1ED7C24400BAB307 /* Release */,
497 | );
498 | defaultConfigurationIsVisible = 0;
499 | defaultConfigurationName = Release;
500 | };
501 | 333C3D611ED7C2A700BAB307 /* Build configuration list for PBXNativeTarget "Example" */ = {
502 | isa = XCConfigurationList;
503 | buildConfigurations = (
504 | 333C3D621ED7C2A700BAB307 /* Debug */,
505 | 333C3D631ED7C2A700BAB307 /* Release */,
506 | );
507 | defaultConfigurationIsVisible = 0;
508 | defaultConfigurationName = Release;
509 | };
510 | /* End XCConfigurationList section */
511 | };
512 | rootObject = 333C3D3A1ED7C24400BAB307 /* Project object */;
513 | }
514 |
--------------------------------------------------------------------------------
/VGSegment.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/VGSegment.xcodeproj/xcshareddata/xcschemes/VGSegment.xcscheme:
--------------------------------------------------------------------------------
1 |
2 |
5 |
8 |
9 |
15 |
21 |
22 |
23 |
24 |
25 |
31 |
32 |
33 |
34 |
35 |
36 |
47 |
48 |
54 |
55 |
56 |
57 |
58 |
59 |
65 |
66 |
72 |
73 |
74 |
75 |
77 |
78 |
81 |
82 |
83 |
--------------------------------------------------------------------------------
/VGSegment/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | FMWK
17 | CFBundleShortVersionString
18 | 1.0.1
19 | CFBundleVersion
20 | $(CURRENT_PROJECT_VERSION)
21 | NSPrincipalClass
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/VGSegment/VGSegment+Animation.swift:
--------------------------------------------------------------------------------
1 | //
2 | // VGSegment+Animation.swift
3 | // VGSegment
4 | //
5 | // Created by Vein on 2017/5/28.
6 | // Copyright © 2017年 Vein. All rights reserved.
7 | //
8 |
9 | import UIKit
10 | // move animation
11 | extension VGSegment {
12 | func moveSelectionIndicator(_ indicator: UIView, fromeLabel: UILabel, toLabel: UILabel, animated: Bool) {
13 |
14 | let normalTitleColor = configuration.normalTitleColor
15 | let selectedTitleColor = configuration.selectedTitleColor
16 | let normalTitleFont = configuration.normalTitleFont
17 | let selectedTitleFont = configuration.selectedTitleFont
18 | let animationDuration = configuration.animationDuration / 2
19 |
20 | let style = configuration.segmentStyle
21 |
22 | fromeLabel.textColor = normalTitleColor
23 | toLabel.textColor = selectedTitleColor
24 |
25 | let fromeFrame = fromeLabel.frame
26 | let toFrame = toLabel.frame
27 |
28 | UIView.animate(withDuration: animationDuration) {
29 | fromeLabel.font = normalTitleFont
30 | toLabel.font = selectedTitleFont
31 | };
32 |
33 | switch style {
34 | case .line:
35 | if animated {
36 | UIView.animate(withDuration: animationDuration, animations: {
37 | indicator.frame.origin.x = toLabel.frame.origin.x
38 | indicator.frame.size.width = toLabel.frame.width
39 | })
40 | } else {
41 | indicator.frame.origin.x = toLabel.frame.origin.x
42 | indicator.frame.size.width = toLabel.frame.width
43 | }
44 |
45 | case .caterpillarLines:
46 | if animated {
47 | if fromeFrame.origin.x < toFrame.origin.x {
48 |
49 | UIView.animate(withDuration: animationDuration, delay: 0, options: .curveLinear, animations: {
50 | indicator.frame.size.width = fromeFrame.width + toFrame.width
51 | }, completion: { (completion) in
52 | UIView.animate(withDuration: animationDuration, animations: {
53 | indicator.frame.size.width = toFrame.width
54 | indicator.frame.origin.x = toFrame.origin.x
55 | })
56 | })
57 |
58 | } else {
59 |
60 | UIView.animate(withDuration: animationDuration, delay: 0, options: .curveLinear, animations: {
61 | indicator.frame.origin.x = toFrame.origin.x
62 | indicator.frame.size.width = fromeFrame.width + toFrame.width
63 | }, completion: { (completion) in
64 | UIView.animate(withDuration: animationDuration, animations: {
65 | indicator.frame.size.width = toFrame.width
66 | })
67 | })
68 |
69 | }
70 | } else {
71 | indicator.frame.origin.x = toLabel.frame.origin.x
72 | indicator.frame.size.width = toLabel.frame.width
73 | }
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/VGSegment/VGSegment+UITapGestureRecognizer.swift:
--------------------------------------------------------------------------------
1 | //
2 | // VGSegment+UITapGestureRecognizer.swift
3 | // VGSegment
4 | //
5 | // Created by Vein on 2017/5/27.
6 | // Copyright © 2017年 Vein. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | extension VGSegment : UIGestureRecognizerDelegate {
12 | @objc func handleSegmentTap(_ gestureRecognizer : UITapGestureRecognizer) {
13 | let tappedPointX : CGFloat = gestureRecognizer.location(in: self).x + scrollView.contentOffset.x
14 |
15 | for (i, label) in titleLabels.enumerated() {
16 | if tappedPointX >= label.frame.minX && tappedPointX <= label.frame.maxX {
17 | //move
18 | selectionTitle(index: i)
19 | }
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/VGSegment/VGSegment.h:
--------------------------------------------------------------------------------
1 | //
2 | // VGSegment.h
3 | // VGSegment
4 | //
5 | // Created by Vein on 2017/5/26.
6 | // Copyright © 2017年 Vein. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | //! Project version number for VGSegment.
12 | FOUNDATION_EXPORT double VGSegmentVersionNumber;
13 |
14 | //! Project version string for VGSegment.
15 | FOUNDATION_EXPORT const unsigned char VGSegmentVersionString[];
16 |
17 | // In this header, you should import all the public headers of your framework using statements like #import
18 |
19 |
20 |
--------------------------------------------------------------------------------
/VGSegment/VGSegment.swift:
--------------------------------------------------------------------------------
1 | //
2 | // VGSegment.swift
3 | // VGSegment
4 | //
5 | // Created by Vein on 2017/5/26.
6 | // Copyright © 2017年 Vein. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | @objc public protocol VGSegmentDelegate {
12 | // MARK: - Delegate func
13 | @objc optional func didSelectAtIndex(_ index: Int)
14 | }
15 |
16 | open class VGSegment: UIView{
17 | open var configuration = VGSegmentConfiguration () {
18 | didSet{
19 | reloadView()
20 | }
21 | }
22 | open var titles:[String] {
23 | didSet{
24 | guard oldValue != titles else { return }
25 | reloadView()
26 | }
27 | }
28 |
29 | open weak var delegate: VGSegmentDelegate?
30 | public var titleLabels: [UILabel] = []
31 | fileprivate var selectIndex: Int = 0
32 | fileprivate let indicator: UIView = {
33 | let view = UIView()
34 | view.layer.masksToBounds = true
35 | return view
36 | }()
37 |
38 | public let scrollView: UIScrollView = {
39 | let view = UIScrollView()
40 | view.bounces = true
41 | view.isPagingEnabled = false
42 | view.isScrollEnabled = true
43 | view.scrollsToTop = false
44 | view.showsVerticalScrollIndicator = false
45 | view.showsHorizontalScrollIndicator = false
46 | return view
47 | }()
48 |
49 | public convenience init(frame: CGRect, titles: [String] ) {
50 | self.init(frame:frame, segmentConfiguration: VGSegmentConfiguration(), titles: titles)
51 | }
52 |
53 | public init(frame: CGRect, segmentConfiguration: VGSegmentConfiguration, titles: [String]) {
54 | self.configuration = segmentConfiguration
55 | self.titles = titles;
56 | super.init(frame: frame)
57 | addSubview(UIView())
58 | addSubview(scrollView)
59 | reloadView()
60 | }
61 |
62 | required public init?(coder aDecoder: NSCoder) {
63 | fatalError("init(coder:) has not been implemented")
64 | }
65 |
66 | open override func layoutSubviews() {
67 | super.layoutSubviews()
68 | scrollView.frame = bounds
69 | let selectLabel = titleLabels[selectIndex]
70 | let offsetX = min(max(0, selectLabel.center.x - bounds.width / 2),
71 | max(0, scrollView.contentSize.width - bounds.width))
72 | scrollView.setContentOffset(CGPoint(x:offsetX, y: 0), animated: false)
73 | }
74 |
75 | }
76 |
77 | //MARK: - public
78 | extension VGSegment {
79 |
80 | public func selectionTitle(index: Int,animated: Bool = true) {
81 | if index >= 0 && index < titleLabels.count && selectIndex != index {
82 |
83 | let preSelectLabel = titleLabels[selectIndex]
84 | let selectLabel = titleLabels[index]
85 | let offsetX = min(max(0, selectLabel.center.x - bounds.width / 2),
86 | max(0, scrollView.contentSize.width - bounds.width))
87 | scrollView.setContentOffset(CGPoint(x:offsetX, y: 0), animated: true)
88 | // move
89 | moveSelectionIndicator(indicator, fromeLabel: preSelectLabel, toLabel: selectLabel, animated: animated)
90 | selectIndex = index
91 | delegate?.didSelectAtIndex?(index)
92 | configuration.segmentSelectIndex = index
93 | }
94 | }
95 | }
96 |
97 | //MARK: - private
98 | extension VGSegment {
99 |
100 | func labelSizeToFit(_ text: String, font: UIFont) -> CGFloat {
101 | let titleText = text as String
102 | let width = titleText.boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: 0.0), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil).width
103 | return width
104 | }
105 |
106 | func setupTitleLabel(_ title: String, font: UIFont, index: Int, color: UIColor, frame: CGRect) {
107 | let titleLabel = UILabel(frame: CGRect.zero)
108 | titleLabel.tag = index
109 | titleLabel.text = title
110 | titleLabel.textColor = color
111 | titleLabel.font = font
112 | titleLabel.frame = frame
113 | titleLabel.textAlignment = .center
114 | titleLabels.append(titleLabel)
115 | scrollView.addSubview(titleLabel)
116 | }
117 |
118 | func setupIndicator(_ frame: CGRect) {
119 | if configuration.hideIndicator { return }
120 |
121 | indicator.backgroundColor = configuration.indicatorColor
122 | indicator.layer.cornerRadius = configuration.indicatorCornerRadius
123 |
124 | let height = configuration.indicatorHeight
125 |
126 | indicator.frame = frame
127 | indicator.frame.origin.y = bounds.height - height
128 | indicator.frame.size.height = height
129 |
130 | scrollView.addSubview(indicator)
131 | }
132 |
133 | fileprivate func reloadView() {
134 |
135 | cleanSegemtn()
136 |
137 | scrollView.frame = bounds;
138 | scrollView.backgroundColor = configuration.segmentBackgroundColor
139 |
140 | var font = configuration.normalTitleFont
141 | let margin = configuration.segmentTitleMargin
142 | var titleColor = configuration.normalTitleColor
143 |
144 | for (i, title) in titles.enumerated() {
145 | let labelX = (titleLabels.last?.frame.maxX ?? 0)
146 | let labelY = (bounds.maxY - font.lineHeight) / 2
147 | let width = labelSizeToFit(title, font: font) + margin * 2
148 | let labelFrame = CGRect(x: labelX, y: labelY , width: width, height: font.lineHeight)
149 |
150 | if i == configuration.segmentSelectIndex {
151 | selectIndex = i
152 | titleColor = configuration.selectedTitleColor
153 | font = configuration.selectedTitleFont
154 | setupIndicator(labelFrame)
155 | } else {
156 | titleColor = configuration.normalTitleColor
157 | font = configuration.normalTitleFont
158 | }
159 |
160 | setupTitleLabel(title, font: font, index: i, color: titleColor, frame: labelFrame)
161 | }
162 |
163 | scrollView.contentSize.width = titleLabels.last!.frame.maxX
164 | let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(VGSegment.handleSegmentTap))
165 | addGestureRecognizer(tapGestureRecognizer)
166 | }
167 |
168 | fileprivate func cleanSegemtn() {
169 | titleLabels.removeAll()
170 | scrollView.subviews.forEach { $0.removeFromSuperview() }
171 | }
172 | }
173 |
--------------------------------------------------------------------------------
/VGSegment/VGSegmentConfiguration.swift:
--------------------------------------------------------------------------------
1 | //
2 | // VGSegmentConfiguration.swift
3 | // VGSegment
4 | //
5 | // Created by Vein on 2017/5/26.
6 | // Copyright © 2017年 Vein. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | @objc public enum VGSegmentStyle: Int{
12 | case line
13 | case caterpillarLines
14 | }
15 |
16 | public class VGSegmentConfiguration {
17 |
18 | open var segmentBackgroundColor : UIColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
19 | open var segmentTitleMargin : CGFloat = 15.0
20 | open var segmentStyle : VGSegmentStyle = .caterpillarLines
21 | open var normalTitleColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
22 | open var selectedTitleColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
23 | open var normalTitleFont : UIFont = UIFont.boldSystemFont(ofSize: 14)
24 | open var selectedTitleFont : UIFont = UIFont.boldSystemFont(ofSize: 16)
25 | open var segmentTitleFontEnlargeScale : CGFloat = 0.2
26 |
27 | open var indicatorColor : UIColor = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)
28 | open var indicatorHeight : CGFloat = 2.0
29 | open var indicatorCornerRadius : CGFloat = 2.0
30 | open var hideIndicator : Bool = false
31 |
32 | open var segmentSelectIndex : Int = 0
33 | open var animationDuration : TimeInterval = 0.3
34 |
35 | public init() {
36 |
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/demo1.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VeinGuo/VGSegment/6a5ff0c450a2fb50495d627481ce64bae349ebaf/demo1.gif
--------------------------------------------------------------------------------
/demo2.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VeinGuo/VGSegment/6a5ff0c450a2fb50495d627481ce64bae349ebaf/demo2.gif
--------------------------------------------------------------------------------