gme_2
6 years ago
44 changed files with 8394 additions and 4633 deletions
-
6GMERemittance.xcodeproj/project.pbxproj
-
6Podfile.lock
-
6Pods/Manifest.lock
-
9604Pods/Pods.xcodeproj/project.pbxproj
-
25Pods/Target Support Files/Pods-GMERemittance/Pods-GMERemittance-acknowledgements.markdown
-
31Pods/Target Support Files/Pods-GMERemittance/Pods-GMERemittance-acknowledgements.plist
-
2Pods/Target Support Files/Pods-GMERemittance/Pods-GMERemittance-frameworks.sh
-
6Pods/Target Support Files/Pods-GMERemittance/Pods-GMERemittance.debug.xcconfig
-
6Pods/Target Support Files/Pods-GMERemittance/Pods-GMERemittance.release.xcconfig
-
25Pods/Target Support Files/Pods-GMERemittanceTests/Pods-GMERemittanceTests-acknowledgements.markdown
-
31Pods/Target Support Files/Pods-GMERemittanceTests/Pods-GMERemittanceTests-acknowledgements.plist
-
2Pods/Target Support Files/Pods-GMERemittanceTests/Pods-GMERemittanceTests-frameworks.sh
-
6Pods/Target Support Files/Pods-GMERemittanceTests/Pods-GMERemittanceTests.debug.xcconfig
-
6Pods/Target Support Files/Pods-GMERemittanceTests/Pods-GMERemittanceTests.release.xcconfig
-
25Pods/Target Support Files/Pods-GMERemittanceUITests/Pods-GMERemittanceUITests-acknowledgements.markdown
-
31Pods/Target Support Files/Pods-GMERemittanceUITests/Pods-GMERemittanceUITests-acknowledgements.plist
-
2Pods/Target Support Files/Pods-GMERemittanceUITests/Pods-GMERemittanceUITests-frameworks.sh
-
6Pods/Target Support Files/Pods-GMERemittanceUITests/Pods-GMERemittanceUITests.debug.xcconfig
-
6Pods/Target Support Files/Pods-GMERemittanceUITests/Pods-GMERemittanceUITests.release.xcconfig
-
26Pods/Target Support Files/XLPagerTabStrip/Info.plist
-
24Pods/Target Support Files/XLPagerTabStrip/ResourceBundle-XLPagerTabStrip-Info.plist
-
5Pods/Target Support Files/XLPagerTabStrip/XLPagerTabStrip-dummy.m
-
12Pods/Target Support Files/XLPagerTabStrip/XLPagerTabStrip-prefix.pch
-
17Pods/Target Support Files/XLPagerTabStrip/XLPagerTabStrip-umbrella.h
-
6Pods/Target Support Files/XLPagerTabStrip/XLPagerTabStrip.modulemap
-
10Pods/Target Support Files/XLPagerTabStrip/XLPagerTabStrip.xcconfig
-
21Pods/XLPagerTabStrip/LICENSE
-
345Pods/XLPagerTabStrip/README.md
-
98Pods/XLPagerTabStrip/Sources/BarPagerTabStripViewController.swift
-
91Pods/XLPagerTabStrip/Sources/BarView.swift
-
352Pods/XLPagerTabStrip/Sources/BaseButtonBarPagerTabStripViewController.swift
-
415Pods/XLPagerTabStrip/Sources/ButtonBarPagerTabStripViewController.swift
-
191Pods/XLPagerTabStrip/Sources/ButtonBarView.swift
-
53Pods/XLPagerTabStrip/Sources/ButtonBarViewCell.swift
-
50Pods/XLPagerTabStrip/Sources/ButtonCell.xib
-
106Pods/XLPagerTabStrip/Sources/FXPageControl.h
-
432Pods/XLPagerTabStrip/Sources/FXPageControl.m
-
80Pods/XLPagerTabStrip/Sources/IndicatorInfo.swift
-
58Pods/XLPagerTabStrip/Sources/PagerTabStripBehaviour.swift
-
31Pods/XLPagerTabStrip/Sources/PagerTabStripError.swift
-
396Pods/XLPagerTabStrip/Sources/PagerTabStripViewController.swift
-
111Pods/XLPagerTabStrip/Sources/SegmentedPagerTabStripViewController.swift
-
31Pods/XLPagerTabStrip/Sources/SwipeDirection.swift
-
234Pods/XLPagerTabStrip/Sources/TwitterPagerTabStripViewController.swift
9604
Pods/Pods.xcodeproj/project.pbxproj
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,26 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
|||
<plist version="1.0"> |
|||
<dict> |
|||
<key>CFBundleDevelopmentRegion</key> |
|||
<string>en</string> |
|||
<key>CFBundleExecutable</key> |
|||
<string>${EXECUTABLE_NAME}</string> |
|||
<key>CFBundleIdentifier</key> |
|||
<string>${PRODUCT_BUNDLE_IDENTIFIER}</string> |
|||
<key>CFBundleInfoDictionaryVersion</key> |
|||
<string>6.0</string> |
|||
<key>CFBundleName</key> |
|||
<string>${PRODUCT_NAME}</string> |
|||
<key>CFBundlePackageType</key> |
|||
<string>FMWK</string> |
|||
<key>CFBundleShortVersionString</key> |
|||
<string>8.0.1</string> |
|||
<key>CFBundleSignature</key> |
|||
<string>????</string> |
|||
<key>CFBundleVersion</key> |
|||
<string>${CURRENT_PROJECT_VERSION}</string> |
|||
<key>NSPrincipalClass</key> |
|||
<string></string> |
|||
</dict> |
|||
</plist> |
@ -0,0 +1,24 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
|||
<plist version="1.0"> |
|||
<dict> |
|||
<key>CFBundleDevelopmentRegion</key> |
|||
<string>en</string> |
|||
<key>CFBundleIdentifier</key> |
|||
<string>${PRODUCT_BUNDLE_IDENTIFIER}</string> |
|||
<key>CFBundleInfoDictionaryVersion</key> |
|||
<string>6.0</string> |
|||
<key>CFBundleName</key> |
|||
<string>${PRODUCT_NAME}</string> |
|||
<key>CFBundlePackageType</key> |
|||
<string>BNDL</string> |
|||
<key>CFBundleShortVersionString</key> |
|||
<string>8.0.1</string> |
|||
<key>CFBundleSignature</key> |
|||
<string>????</string> |
|||
<key>CFBundleVersion</key> |
|||
<string>1</string> |
|||
<key>NSPrincipalClass</key> |
|||
<string></string> |
|||
</dict> |
|||
</plist> |
@ -0,0 +1,5 @@ |
|||
#import <Foundation/Foundation.h> |
|||
@interface PodsDummy_XLPagerTabStrip : NSObject |
|||
@end |
|||
@implementation PodsDummy_XLPagerTabStrip |
|||
@end |
@ -0,0 +1,12 @@ |
|||
#ifdef __OBJC__ |
|||
#import <UIKit/UIKit.h> |
|||
#else |
|||
#ifndef FOUNDATION_EXPORT |
|||
#if defined(__cplusplus) |
|||
#define FOUNDATION_EXPORT extern "C" |
|||
#else |
|||
#define FOUNDATION_EXPORT extern |
|||
#endif |
|||
#endif |
|||
#endif |
|||
|
@ -0,0 +1,17 @@ |
|||
#ifdef __OBJC__ |
|||
#import <UIKit/UIKit.h> |
|||
#else |
|||
#ifndef FOUNDATION_EXPORT |
|||
#if defined(__cplusplus) |
|||
#define FOUNDATION_EXPORT extern "C" |
|||
#else |
|||
#define FOUNDATION_EXPORT extern |
|||
#endif |
|||
#endif |
|||
#endif |
|||
|
|||
#import "FXPageControl.h" |
|||
|
|||
FOUNDATION_EXPORT double XLPagerTabStripVersionNumber; |
|||
FOUNDATION_EXPORT const unsigned char XLPagerTabStripVersionString[]; |
|||
|
@ -0,0 +1,6 @@ |
|||
framework module XLPagerTabStrip { |
|||
umbrella header "XLPagerTabStrip-umbrella.h" |
|||
|
|||
export * |
|||
module * { export * } |
|||
} |
@ -0,0 +1,10 @@ |
|||
CONFIGURATION_BUILD_DIR = ${PODS_CONFIGURATION_BUILD_DIR}/XLPagerTabStrip |
|||
GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1 |
|||
OTHER_LDFLAGS = -framework "Foundation" -framework "UIKit" |
|||
OTHER_SWIFT_FLAGS = $(inherited) "-D" "COCOAPODS" |
|||
PODS_BUILD_DIR = ${BUILD_DIR} |
|||
PODS_CONFIGURATION_BUILD_DIR = ${PODS_BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME) |
|||
PODS_ROOT = ${SRCROOT} |
|||
PODS_TARGET_SRCROOT = ${PODS_ROOT}/XLPagerTabStrip |
|||
PRODUCT_BUNDLE_IDENTIFIER = org.cocoapods.${PRODUCT_NAME:rfc1034identifier} |
|||
SKIP_INSTALL = YES |
@ -0,0 +1,21 @@ |
|||
The MIT License (MIT) |
|||
|
|||
Copyright (c) 2017 Xmartlabs SRL |
|||
|
|||
Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
of this software and associated documentation files (the "Software"), to deal |
|||
in the Software without restriction, including without limitation the rights |
|||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
copies of the Software, and to permit persons to whom the Software is |
|||
furnished to do so, subject to the following conditions: |
|||
|
|||
The above copyright notice and this permission notice shall be included in all |
|||
copies or substantial portions of the Software. |
|||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|||
SOFTWARE. |
@ -0,0 +1,345 @@ |
|||
# XLPagerTabStrip |
|||
|
|||
<p align="left"> |
|||
<a href="https://travis-ci.org/xmartlabs/XLPagerTabStrip"><img src="https://travis-ci.org/xmartlabs/XLPagerTabStrip.svg?branch=master" alt="Build status" /></a> |
|||
<img src="https://img.shields.io/badge/platform-iOS-blue.svg?style=flat" alt="Platform iOS" /> |
|||
<a href="https://developer.apple.com/swift"><img src="https://img.shields.io/badge/swift4-compatible-4BC51D.svg?style=flat" alt="Swift 3 compatible" /></a> |
|||
<a href="https://github.com/Carthage/Carthage"><img src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat" alt="Carthage compatible" /></a> |
|||
<a href="https://cocoapods.org/pods/XLPagerTabStrip"><img src="https://img.shields.io/cocoapods/v/XLPagerTabStrip.svg" alt="CocoaPods compatible" /></a> |
|||
<a href="https://raw.githubusercontent.com/xmartlabs/XLPagerTabStrip/master/LICENSE"><img src="http://img.shields.io/badge/license-MIT-blue.svg?style=flat" alt="License: MIT" /> |
|||
</a> |
|||
<!-- <a href="https://codebeat.co/projects/github-com-xmartlabs-xlpagertabstrip"><img alt="codebeat badge" src="https://codebeat.co/badges/f32c9ad3-0aa1-4b40-a632-9421211bd39e" /></a> --> |
|||
|
|||
</p> |
|||
|
|||
Made with ❤️ by [XMARTLABS](http://xmartlabs.com). |
|||
|
|||
Android [PagerTabStrip](http://developer.android.com/reference/android/support/v4/view/PagerTabStrip.html) for iOS! |
|||
|
|||
**XLPagerTabStrip** is a *Container View Controller* that allows us to switch easily among a collection of view controllers. Pan gesture can be used to move on to next or previous view controller. It shows a interactive indicator of the current, previous, next child view controllers. |
|||
|
|||
<table> |
|||
<tr> |
|||
<th><img src="Example/instagram.gif" width="250"/></th> |
|||
<th><img src="Example/spotify.gif" width="250"/></th> |
|||
<th><img src="Example/youtube.gif" width="250"/></th> |
|||
<th><img src="Example/pagerTabStripTypes.gif" width="250"/></th> |
|||
</tr> |
|||
</table> |
|||
|
|||
## Getting involved |
|||
|
|||
* If you **want to contribute** please feel free to **submit pull requests**. |
|||
* If you **have a feature request** please **open an issue**. |
|||
* If you **found a bug** or **need help** please **check older issues, [FAQ](#faq) and threads on [StackOverflow](http://stackoverflow.com/questions/tagged/XLPagerTabStrip) (Tag 'XLPagerTabStrip') before submitting an issue**. |
|||
|
|||
**Before contribute check the [CONTRIBUTING](CONTRIBUTING.md) file for more info.** |
|||
|
|||
If you use **XLPagerTabStrip** in your app We would love to hear about it! Drop us a line on [twitter](https://twitter.com/xmartlabs). |
|||
|
|||
## Pager Types |
|||
|
|||
The library provides 4 different ways to show the view controllers. |
|||
|
|||
### Button Bar |
|||
|
|||
This is likely to be the most common pager type. It's used by many well known apps such as instagram, youtube, skype and many others. |
|||
|
|||
<img src="Example/barButton.gif" width="250"/> |
|||
|
|||
### Bar |
|||
|
|||
This mode doesn't show a title neither an image. It only shows a bar that indicates the current view controller. |
|||
|
|||
<img src="Example/bar.gif" width="250"/> |
|||
|
|||
### Twitter |
|||
|
|||
Long time ago twitter app made use of this type of pager in the app main screen. |
|||
|
|||
<img src="Example/twitter.gif" width="250"/> |
|||
|
|||
### Segmented |
|||
|
|||
This mode uses a `UISegmentedControl` to indicates which is the view controller being displayed. |
|||
|
|||
<img src="Example/segmented.gif" width="250"/> |
|||
|
|||
## Usage |
|||
|
|||
Basically we just need to provide the list of child view controllers to show and these view controllers should provide the information (title or image) that will be shown in the associated indicator. |
|||
|
|||
Let's see the steps to do this: |
|||
|
|||
##### Choose which type of pager we want to create |
|||
|
|||
First we should choose the type of pager we want to create, depending on our choice we will have to create a view controller that extends from one of the following controllers: `TwitterPagerTabStripViewController`, `ButtonBarPagerTabStripViewController`, `SegmentedPagerTabStripViewController`, `BarPagerTabStripViewController`. |
|||
|
|||
> All these build-in pager controllers extend from the base class `PagerTabStripViewController`. |
|||
> You can also make your custom pager controller by extending directly from `PagerTabStripViewController` in case no pager menu type fits your needs. |
|||
|
|||
```swift |
|||
import XLPagerTabStrip |
|||
|
|||
class MyPagerTabStripName: ButtonBarPagerTabStripViewController { |
|||
.. |
|||
} |
|||
``` |
|||
|
|||
##### Connect outlets and add layout constraints |
|||
|
|||
We strongly recommend to use IB to set up our page controller views. |
|||
|
|||
Drag into the storyboard a `UIViewController` and set up its class with your pager controller (`MyPagerTabStripName`). |
|||
Drag a `UIScrollView` into your view controller view and connect `PagerTabStripViewController` `containerView` outlet with the scroll view. |
|||
|
|||
Depending on which type of paging view controller you are working with you may have to connect more outlets. |
|||
|
|||
For `BarPagerTabStripViewController` we should connect `barView` outlet. barView type is UIView. `ButtonBarPagerTabStripViewController` requires us to connect `buttonBarView` outlet. `buttonBarView` type is `ButtonBarView` which extends from `UICollectionView`. `SegmentedPagerTabStripViewController` has a `segmentedControl` outlet, if the outlet is not connected the library try to set up the navigationItem `titleView` property using a `UISegmentedControl`. `TwitterPagerTabStripViewController` doesn't require us to connect any additional outlet. |
|||
|
|||
> The example project contains a example for each pager controller type and we can look into it to see how views were added and how outlets were connected. |
|||
|
|||
##### Provide the view controllers that will appear embedded into the PagerTabStrip view controller |
|||
|
|||
You can provide the view controllers by overriding `func viewControllers(for: pagerTabStripController: PagerTabStripViewController) -> [UIViewController]` method. |
|||
|
|||
```swift |
|||
override public func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { |
|||
return [MyEmbeddedViewController(), MySecondEmbeddedViewController()] |
|||
} |
|||
``` |
|||
|
|||
> The method above is the only method declared in `PagerTabStripDataSource` protocol. We don't need to explicitly conform to it since base pager class already does it. |
|||
|
|||
|
|||
##### Provide information to show in each indicator |
|||
|
|||
Every UIViewController that will appear within the PagerTabStrip needs to provide either a title or an image. |
|||
In order to do so they should conform to `IndicatorInfoProvider` by implementing `func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo` |
|||
which provides the information required to show the PagerTabStrip menu (indicator) associated with the view controller. |
|||
|
|||
```swift |
|||
class MyEmbeddedViewController: UITableViewController, IndicatorInfoProvider { |
|||
|
|||
func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo { |
|||
return IndicatorInfo(title: "My Child title") |
|||
} |
|||
} |
|||
``` |
|||
|
|||
**For a detailed step by step guide about how to use the library, please check out this community [blog post](https://medium.com/michaeladeyeri/how-to-implement-android-like-tab-layouts-in-ios-using-swift-3-578516c3aa9).** |
|||
|
|||
That's it! We're done! 🍻🍻 |
|||
|
|||
|
|||
## Customization |
|||
|
|||
##### Pager Behaviour |
|||
|
|||
The pager indicator can be updated progressive as we swipe or at once in the middle of the transition between the view controllers. |
|||
By setting up `pagerBehaviour` property we can choose how the indicator should be updated. |
|||
|
|||
```swift |
|||
public var pagerBehaviour: PagerTabStripBehaviour |
|||
``` |
|||
|
|||
```swift |
|||
public enum PagerTabStripBehaviour { |
|||
case Common(skipIntermediteViewControllers: Bool) |
|||
case Progressive(skipIntermediteViewControllers: Bool, elasticIndicatorLimit: Bool) |
|||
} |
|||
``` |
|||
|
|||
Default Values: |
|||
```swift |
|||
// Twitter Type |
|||
PagerTabStripBehaviour.Common(skipIntermediteViewControllers: true) |
|||
// Segmented Type |
|||
PagerTabStripBehaviour.Common(skipIntermediteViewControllers: true) |
|||
// Bar Type |
|||
PagerTabStripBehaviour.Progressive(skipIntermediteViewControllers: true, elasticIndicatorLimit: true) |
|||
// ButtonBar Type |
|||
PagerTabStripBehaviour.Progressive(skipIntermediteViewControllers: true, elasticIndicatorLimit: true)` |
|||
``` |
|||
|
|||
As you might have noticed `Common` and `Progressive` enumeration cases has `skipIntermediteViewControllers` and `elasticIndicatorLimit` associated values. |
|||
|
|||
`skipIntermediteViewControllers` allows us to skip intermediate view controllers when a tab indicator is tapped. |
|||
|
|||
`elasticIndicatorLimit` allows us to tension the indicator when we reach a limit, I mean when we try to move forward from last indicator or move back from first indicator. |
|||
|
|||
##### PagerTabStripDelegate & PagerTabStripIsProgressiveDelegate |
|||
|
|||
Normally we don't need to implement these protocols because each pager type already conforms to it in order to properly update its indicator. Anyway there may be some scenarios when overriding a method come come in handy. |
|||
|
|||
```swift |
|||
public protocol PagerTabStripDelegate: class { |
|||
|
|||
func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int) |
|||
} |
|||
|
|||
public protocol PagerTabStripIsProgressiveDelegate : PagerTabStripDelegate { |
|||
|
|||
func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool) |
|||
} |
|||
``` |
|||
|
|||
> Again, The method invoked by the library depends on the `pagerBehaviour` value. |
|||
|
|||
|
|||
|
|||
|
|||
### ButtonBar Customization |
|||
|
|||
```swift |
|||
|
|||
settings.style.buttonBarBackgroundColor: UIColor? |
|||
// buttonBar minimumInteritemSpacing value, note that button bar extends from UICollectionView |
|||
settings.style.buttonBarMinimumInteritemSpacing: CGFloat? |
|||
// buttonBar minimumLineSpacing value |
|||
settings.style.buttonBarMinimumLineSpacing: CGFloat? |
|||
// buttonBar flow layout left content inset value |
|||
settings.style.buttonBarLeftContentInset: CGFloat? |
|||
// buttonBar flow layout right content inset value |
|||
settings.style.buttonBarRightContentInset: CGFloat? |
|||
|
|||
// selected bar view is created programmatically so it's important to set up the following 2 properties properly |
|||
settings.style.selectedBarBackgroundColor = UIColor.blackColor() |
|||
settings.style.selectedBarHeight: CGFloat = 5 |
|||
|
|||
// each buttonBar item is a UICollectionView cell of type ButtonBarViewCell |
|||
settings.style.buttonBarItemBackgroundColor: UIColor? |
|||
settings.style.buttonBarItemFont = UIFont.systemFontOfSize(18) |
|||
// helps to determine the cell width, it represent the space before and after the title label |
|||
settings.style.buttonBarItemLeftRightMargin: CGFloat = 8 |
|||
settings.style.buttonBarItemTitleColor: UIColor? |
|||
// in case the barView items do not fill the screen width this property stretch the cells to fill the screen |
|||
settings.style.buttonBarItemsShouldFillAvailiableWidth = true |
|||
// only used if button bar is created programmatically and not using storyboards or nib files as recommended. |
|||
public var buttonBarHeight: CGFloat? |
|||
``` |
|||
|
|||
**Important:** Settings should be called before `viewDidLoad` is called. |
|||
```swift |
|||
override func viewDidLoad() { |
|||
self.settings.style.selectedBarHeight = 2 |
|||
self.settings.style.selectedBarBackgroundColor = UIColor.whiteColor() |
|||
|
|||
super.viewDidLoad() |
|||
} |
|||
``` |
|||
|
|||
##### Update cells when selected indicator changes |
|||
|
|||
We may need to update the indicator cell when the displayed view controller changes. The following function properties help to accomplish that. Depending on our pager `pagerBehaviour` value we will have to set up `changeCurrentIndex` or `changeCurrentIndexProgressive`. |
|||
|
|||
```swift |
|||
public var changeCurrentIndex: ((oldCell: ButtonBarViewCell?, newCell: ButtonBarViewCell?, animated: Bool) -> Void)? |
|||
public var changeCurrentIndexProgressive: ((oldCell: ButtonBarViewCell?, newCell: ButtonBarViewCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool, animated: Bool) -> Void)? |
|||
``` |
|||
|
|||
Let's see an example: |
|||
|
|||
```swift |
|||
changeCurrentIndexProgressive = { (oldCell: ButtonBarViewCell?, newCell: ButtonBarViewCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool, animated: Bool) -> Void in |
|||
guard changeCurrentIndex == true else { return } |
|||
|
|||
oldCell?.label.textColor = UIColor(white: 1, alpha: 0.6) |
|||
newCell?.label.textColor = .whiteColor() |
|||
|
|||
if animated { |
|||
UIView.animateWithDuration(0.1, animations: { () -> Void in |
|||
newCell?.transform = CGAffineTransformMakeScale(1.0, 1.0) |
|||
oldCell?.transform = CGAffineTransformMakeScale(0.8, 0.8) |
|||
}) |
|||
} |
|||
else { |
|||
newCell?.transform = CGAffineTransformMakeScale(1.0, 1.0) |
|||
oldCell?.transform = CGAffineTransformMakeScale(0.8, 0.8) |
|||
} |
|||
} |
|||
``` |
|||
|
|||
### Bar Type Customization |
|||
|
|||
```swift |
|||
settings.style.barBackgroundColor: UIColor? |
|||
settings.style.selectedBarBackgroundColor: UIColor? |
|||
// barHeight is only set up when the bar is created programmatically and not using storyboards or xib files as recommended. |
|||
settings.style.barHeight: CGFloat = 5 |
|||
``` |
|||
|
|||
### Twitter Type Customization |
|||
|
|||
```swift |
|||
settings.style.dotColor = UIColor(white: 1, alpha: 0.4) |
|||
settings.style.selectedDotColor = UIColor.whiteColor() |
|||
settings.style.portraitTitleFont = UIFont.systemFontOfSize(18) |
|||
settings.style.landscapeTitleFont = UIFont.systemFontOfSize(15) |
|||
settings.style.titleColor = UIColor.whiteColor() |
|||
``` |
|||
|
|||
### Segmented Type Customization |
|||
|
|||
```swift |
|||
settings.style.segmentedControlColor: UIColor? |
|||
``` |
|||
|
|||
|
|||
|
|||
## Requirements |
|||
|
|||
* iOS 8.0+ |
|||
* Xcode 8.0+ |
|||
|
|||
## Examples |
|||
|
|||
Follow these 3 steps to run Example project: Clone XLPagerTabStrip repository, open XLPagerTabStrip workspace and run the *Example* project. |
|||
|
|||
## Installation |
|||
|
|||
### CocoaPods |
|||
|
|||
[CocoaPods](https://cocoapods.org/) is a dependency manager for Cocoa projects. |
|||
|
|||
To install XLPagerTabStrip, simply add the following line to your Podfile: |
|||
|
|||
```ruby |
|||
pod 'XLPagerTabStrip', '~> 8.0' |
|||
``` |
|||
|
|||
### Carthage |
|||
|
|||
[Carthage](https://github.com/Carthage/Carthage) is a simple, decentralized dependency manager for Cocoa. |
|||
|
|||
To install XLPagerTabStrip, simply add the following line to your Cartfile: |
|||
|
|||
```ogdl |
|||
github "xmartlabs/XLPagerTabStrip" ~> 8.0 |
|||
``` |
|||
|
|||
## FAQ |
|||
|
|||
#### How to change the visible child view controller programmatically |
|||
|
|||
`PagerTabStripViewController` provides the following methods to programmatically change the visible child view controller: |
|||
|
|||
```swift |
|||
func moveToViewController(at index: Int) |
|||
func moveToViewController(at index: Int, animated: Bool) |
|||
func moveTo(viewController: UIViewController) |
|||
func moveTo(viewController: UIViewController, animated: Bool) |
|||
``` |
|||
|
|||
|
|||
#### How to migrate from Swift 2 to Swift 3 <a name="migrate"></a> |
|||
|
|||
Check out [our migration guide](https://github.com/xmartlabs/XLPagerTabStrip/blob/master/Migration.md) |
|||
|
|||
## Author |
|||
|
|||
* [Martin Barreto](https://github.com/mtnBarreto) ([@mtnBarreto](https://twitter.com/mtnBarreto)) |
|||
|
|||
## Change Log |
|||
|
|||
This can be found in the [CHANGELOG.md](CHANGELOG.md) file. |
@ -0,0 +1,98 @@ |
|||
// BarPagerTabStripViewController.swift |
|||
// XLPagerTabStrip ( https://github.com/xmartlabs/XLPagerTabStrip ) |
|||
// |
|||
// Copyright (c) 2017 Xmartlabs ( http://xmartlabs.com ) |
|||
// |
|||
// |
|||
// Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
// of this software and associated documentation files (the "Software"), to deal |
|||
// in the Software without restriction, including without limitation the rights |
|||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
// copies of the Software, and to permit persons to whom the Software is |
|||
// furnished to do so, subject to the following conditions: |
|||
// |
|||
// The above copyright notice and this permission notice shall be included in |
|||
// all copies or substantial portions of the Software. |
|||
// |
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
// THE SOFTWARE. |
|||
|
|||
import Foundation |
|||
import UIKit |
|||
|
|||
public struct BarPagerTabStripSettings { |
|||
|
|||
public struct Style { |
|||
public var barBackgroundColor: UIColor? |
|||
public var selectedBarBackgroundColor: UIColor? |
|||
public var barHeight: CGFloat = 5 // barHeight is ony set up when the bar is created programatically and not using storyboards or xib files. |
|||
} |
|||
|
|||
public var style = Style() |
|||
} |
|||
|
|||
open class BarPagerTabStripViewController: PagerTabStripViewController, PagerTabStripDataSource, PagerTabStripIsProgressiveDelegate { |
|||
|
|||
public var settings = BarPagerTabStripSettings() |
|||
|
|||
@IBOutlet weak public var barView: BarView! |
|||
|
|||
required public init?(coder aDecoder: NSCoder) { |
|||
super.init(coder: aDecoder) |
|||
delegate = self |
|||
datasource = self |
|||
} |
|||
|
|||
public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { |
|||
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) |
|||
delegate = self |
|||
datasource = self |
|||
} |
|||
|
|||
open override func viewDidLoad() { |
|||
super.viewDidLoad() |
|||
barView = barView ?? { |
|||
let barView = BarView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: settings.style.barHeight)) |
|||
barView.autoresizingMask = .flexibleWidth |
|||
barView.backgroundColor = .black |
|||
barView.selectedBar.backgroundColor = .white |
|||
return barView |
|||
}() |
|||
|
|||
barView.backgroundColor = settings.style.barBackgroundColor ?? barView.backgroundColor |
|||
barView.selectedBar.backgroundColor = settings.style.selectedBarBackgroundColor ?? barView.selectedBar.backgroundColor |
|||
} |
|||
|
|||
open override func viewWillAppear(_ animated: Bool) { |
|||
super.viewWillAppear(animated) |
|||
if barView.superview == nil { |
|||
view.addSubview(barView) |
|||
} |
|||
barView.optionsCount = viewControllers.count |
|||
barView.moveTo(index: currentIndex, animated: false) |
|||
} |
|||
|
|||
open override func reloadPagerTabStripView() { |
|||
super.reloadPagerTabStripView() |
|||
barView.optionsCount = viewControllers.count |
|||
if isViewLoaded { |
|||
barView.moveTo(index: currentIndex, animated: false) |
|||
} |
|||
} |
|||
|
|||
// MARK: - PagerTabStripDelegate |
|||
|
|||
open func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool) { |
|||
|
|||
barView.move(fromIndex: fromIndex, toIndex: toIndex, progressPercentage: progressPercentage) |
|||
} |
|||
|
|||
open func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int) { |
|||
barView.moveTo(index: toIndex, animated: true) |
|||
} |
|||
} |
@ -0,0 +1,91 @@ |
|||
// BarView.swift |
|||
// XLPagerTabStrip ( https://github.com/xmartlabs/XLPagerTabStrip ) |
|||
// |
|||
// Copyright (c) 2017 Xmartlabs ( http://xmartlabs.com ) |
|||
// |
|||
// |
|||
// Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
// of this software and associated documentation files (the "Software"), to deal |
|||
// in the Software without restriction, including without limitation the rights |
|||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
// copies of the Software, and to permit persons to whom the Software is |
|||
// furnished to do so, subject to the following conditions: |
|||
// |
|||
// The above copyright notice and this permission notice shall be included in |
|||
// all copies or substantial portions of the Software. |
|||
// |
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
// THE SOFTWARE. |
|||
|
|||
import Foundation |
|||
|
|||
open class BarView: UIView { |
|||
|
|||
open lazy var selectedBar: UIView = { [unowned self] in |
|||
let selectedBar = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)) |
|||
return selectedBar |
|||
}() |
|||
|
|||
var optionsCount = 1 { |
|||
willSet(newOptionsCount) { |
|||
if newOptionsCount <= selectedIndex { |
|||
selectedIndex = optionsCount - 1 |
|||
} |
|||
} |
|||
} |
|||
var selectedIndex = 0 |
|||
|
|||
required public init?(coder aDecoder: NSCoder) { |
|||
super.init(coder: aDecoder) |
|||
addSubview(selectedBar) |
|||
} |
|||
|
|||
override init(frame: CGRect) { |
|||
super.init(frame: frame) |
|||
addSubview(selectedBar) |
|||
} |
|||
|
|||
// MARK: - Helpers |
|||
|
|||
private func updateSelectedBarPosition(with animation: Bool) { |
|||
var frame = selectedBar.frame |
|||
frame.size.width = self.frame.size.width / CGFloat(optionsCount) |
|||
frame.origin.x = frame.size.width * CGFloat(selectedIndex) |
|||
if animation { |
|||
UIView.animate(withDuration: 0.3, animations: { [weak self] in |
|||
self?.selectedBar.frame = frame |
|||
}) |
|||
} else { |
|||
selectedBar.frame = frame |
|||
} |
|||
} |
|||
|
|||
open func moveTo(index: Int, animated: Bool) { |
|||
selectedIndex = index |
|||
updateSelectedBarPosition(with: animated) |
|||
} |
|||
|
|||
open func move(fromIndex: Int, toIndex: Int, progressPercentage: CGFloat) { |
|||
selectedIndex = (progressPercentage > 0.5) ? toIndex : fromIndex |
|||
|
|||
var newFrame = selectedBar.frame |
|||
newFrame.size.width = frame.size.width / CGFloat(optionsCount) |
|||
var fromFrame = newFrame |
|||
fromFrame.origin.x = newFrame.size.width * CGFloat(fromIndex) |
|||
var toFrame = newFrame |
|||
toFrame.origin.x = toFrame.size.width * CGFloat(toIndex) |
|||
var targetFrame = fromFrame |
|||
targetFrame.origin.x += (toFrame.origin.x - targetFrame.origin.x) * CGFloat(progressPercentage) |
|||
selectedBar.frame = targetFrame |
|||
} |
|||
|
|||
open override func layoutSubviews() { |
|||
super.layoutSubviews() |
|||
updateSelectedBarPosition(with: false) |
|||
} |
|||
} |
@ -0,0 +1,352 @@ |
|||
// BaseButtonBarPagerTabStripViewController.swift |
|||
// XLPagerTabStrip ( https://github.com/xmartlabs/XLPagerTabStrip ) |
|||
// |
|||
// Copyright (c) 2017 Xmartlabs ( http://xmartlabs.com ) |
|||
// |
|||
// |
|||
// Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
// of this software and associated documentation files (the "Software"), to deal |
|||
// in the Software without restriction, including without limitation the rights |
|||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
// copies of the Software, and to permit persons to whom the Software is |
|||
// furnished to do so, subject to the following conditions: |
|||
// |
|||
// The above copyright notice and this permission notice shall be included in |
|||
// all copies or substantial portions of the Software. |
|||
// |
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
// THE SOFTWARE. |
|||
|
|||
import Foundation |
|||
|
|||
open class BaseButtonBarPagerTabStripViewController<ButtonBarCellType: UICollectionViewCell>: PagerTabStripViewController, PagerTabStripDataSource, PagerTabStripIsProgressiveDelegate, UICollectionViewDelegate, UICollectionViewDataSource { |
|||
|
|||
public var settings = ButtonBarPagerTabStripSettings() |
|||
public var buttonBarItemSpec: ButtonBarItemSpec<ButtonBarCellType>! |
|||
public var changeCurrentIndex: ((_ oldCell: ButtonBarCellType?, _ newCell: ButtonBarCellType?, _ animated: Bool) -> Void)? |
|||
public var changeCurrentIndexProgressive: ((_ oldCell: ButtonBarCellType?, _ newCell: ButtonBarCellType?, _ progressPercentage: CGFloat, _ changeCurrentIndex: Bool, _ animated: Bool) -> Void)? |
|||
|
|||
@IBOutlet public weak var buttonBarView: ButtonBarView! |
|||
|
|||
lazy private var cachedCellWidths: [CGFloat]? = { [unowned self] in |
|||
return self.calculateWidths() |
|||
}() |
|||
|
|||
public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { |
|||
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) |
|||
delegate = self |
|||
datasource = self |
|||
} |
|||
|
|||
required public init?(coder aDecoder: NSCoder) { |
|||
super.init(coder: aDecoder) |
|||
delegate = self |
|||
datasource = self |
|||
} |
|||
|
|||
open override func viewDidLoad() { |
|||
super.viewDidLoad() |
|||
let buttonBarViewAux = buttonBarView ?? { |
|||
let flowLayout = UICollectionViewFlowLayout() |
|||
flowLayout.scrollDirection = .horizontal |
|||
flowLayout.sectionInset = UIEdgeInsets(top: 0, left: settings.style.buttonBarLeftContentInset ?? 35, bottom: 0, right: settings.style.buttonBarRightContentInset ?? 35) |
|||
let buttonBarHeight = settings.style.buttonBarHeight ?? 44 |
|||
let buttonBar = ButtonBarView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: buttonBarHeight), collectionViewLayout: flowLayout) |
|||
buttonBar.backgroundColor = .orange |
|||
buttonBar.selectedBar.backgroundColor = .black |
|||
buttonBar.autoresizingMask = .flexibleWidth |
|||
var newContainerViewFrame = containerView.frame |
|||
newContainerViewFrame.origin.y = buttonBarHeight |
|||
newContainerViewFrame.size.height = containerView.frame.size.height - (buttonBarHeight - containerView.frame.origin.y) |
|||
containerView.frame = newContainerViewFrame |
|||
return buttonBar |
|||
}() |
|||
buttonBarView = buttonBarViewAux |
|||
|
|||
if buttonBarView.superview == nil { |
|||
view.addSubview(buttonBarView) |
|||
} |
|||
if buttonBarView.delegate == nil { |
|||
buttonBarView.delegate = self |
|||
} |
|||
if buttonBarView.dataSource == nil { |
|||
buttonBarView.dataSource = self |
|||
} |
|||
buttonBarView.scrollsToTop = false |
|||
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout // swiftlint:disable:this force_cast |
|||
flowLayout.scrollDirection = .horizontal |
|||
flowLayout.minimumInteritemSpacing = settings.style.buttonBarMinimumInteritemSpacing ?? flowLayout.minimumInteritemSpacing |
|||
flowLayout.minimumLineSpacing = settings.style.buttonBarMinimumLineSpacing ?? flowLayout.minimumLineSpacing |
|||
let sectionInset = flowLayout.sectionInset |
|||
flowLayout.sectionInset = UIEdgeInsets(top: sectionInset.top, left: settings.style.buttonBarLeftContentInset ?? sectionInset.left, bottom: sectionInset.bottom, right: settings.style.buttonBarRightContentInset ?? sectionInset.right) |
|||
buttonBarView.showsHorizontalScrollIndicator = false |
|||
buttonBarView.backgroundColor = settings.style.buttonBarBackgroundColor ?? buttonBarView.backgroundColor |
|||
buttonBarView.selectedBar.backgroundColor = settings.style.selectedBarBackgroundColor |
|||
|
|||
buttonBarView.selectedBarHeight = settings.style.selectedBarHeight |
|||
// register button bar item cell |
|||
switch buttonBarItemSpec! { |
|||
case .nibFile(let nibName, let bundle, _): |
|||
buttonBarView.register(UINib(nibName: nibName, bundle: bundle), forCellWithReuseIdentifier:"Cell") |
|||
case .cellClass: |
|||
buttonBarView.register(ButtonBarCellType.self, forCellWithReuseIdentifier:"Cell") |
|||
} |
|||
//- |
|||
} |
|||
|
|||
open override func viewWillAppear(_ animated: Bool) { |
|||
super.viewWillAppear(animated) |
|||
buttonBarView.layoutIfNeeded() |
|||
isViewAppearing = true |
|||
} |
|||
|
|||
open override func viewDidAppear(_ animated: Bool) { |
|||
super.viewDidAppear(animated) |
|||
isViewAppearing = false |
|||
} |
|||
|
|||
open override func viewDidLayoutSubviews() { |
|||
super.viewDidLayoutSubviews() |
|||
|
|||
guard isViewAppearing || isViewRotating else { return } |
|||
|
|||
// Force the UICollectionViewFlowLayout to get laid out again with the new size if |
|||
// a) The view is appearing. This ensures that |
|||
// collectionView:layout:sizeForItemAtIndexPath: is called for a second time |
|||
// when the view is shown and when the view *frame(s)* are actually set |
|||
// (we need the view frame's to have been set to work out the size's and on the |
|||
// first call to collectionView:layout:sizeForItemAtIndexPath: the view frame(s) |
|||
// aren't set correctly) |
|||
// b) The view is rotating. This ensures that |
|||
// collectionView:layout:sizeForItemAtIndexPath: is called again and can use the views |
|||
// *new* frame so that the buttonBarView cell's actually get resized correctly |
|||
cachedCellWidths = calculateWidths() |
|||
buttonBarView.collectionViewLayout.invalidateLayout() |
|||
// When the view first appears or is rotated we also need to ensure that the barButtonView's |
|||
// selectedBar is resized and its contentOffset/scroll is set correctly (the selected |
|||
// tab/cell may end up either skewed or off screen after a rotation otherwise) |
|||
buttonBarView.moveTo(index: currentIndex, animated: false, swipeDirection: .none, pagerScroll: .scrollOnlyIfOutOfScreen) |
|||
buttonBarView.selectItem(at: IndexPath(item: currentIndex, section: 0), animated: false, scrollPosition: []) |
|||
} |
|||
|
|||
// MARK: - View Rotation |
|||
|
|||
open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { |
|||
super.viewWillTransition(to: size, with: coordinator) |
|||
} |
|||
|
|||
// MARK: - Public Methods |
|||
|
|||
open override func reloadPagerTabStripView() { |
|||
super.reloadPagerTabStripView() |
|||
guard isViewLoaded else { return } |
|||
buttonBarView.reloadData() |
|||
cachedCellWidths = calculateWidths() |
|||
buttonBarView.moveTo(index: currentIndex, animated: false, swipeDirection: .none, pagerScroll: .yes) |
|||
} |
|||
|
|||
open func calculateStretchedCellWidths(_ minimumCellWidths: [CGFloat], suggestedStretchedCellWidth: CGFloat, previousNumberOfLargeCells: Int) -> CGFloat { |
|||
var numberOfLargeCells = 0 |
|||
var totalWidthOfLargeCells: CGFloat = 0 |
|||
|
|||
for minimumCellWidthValue in minimumCellWidths where minimumCellWidthValue > suggestedStretchedCellWidth { |
|||
totalWidthOfLargeCells += minimumCellWidthValue |
|||
numberOfLargeCells += 1 |
|||
} |
|||
|
|||
guard numberOfLargeCells > previousNumberOfLargeCells else { return suggestedStretchedCellWidth } |
|||
|
|||
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout // swiftlint:disable:this force_cast |
|||
let collectionViewAvailiableWidth = buttonBarView.frame.size.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right |
|||
let numberOfCells = minimumCellWidths.count |
|||
let cellSpacingTotal = CGFloat(numberOfCells - 1) * flowLayout.minimumLineSpacing |
|||
|
|||
let numberOfSmallCells = numberOfCells - numberOfLargeCells |
|||
let newSuggestedStretchedCellWidth = (collectionViewAvailiableWidth - totalWidthOfLargeCells - cellSpacingTotal) / CGFloat(numberOfSmallCells) |
|||
|
|||
return calculateStretchedCellWidths(minimumCellWidths, suggestedStretchedCellWidth: newSuggestedStretchedCellWidth, previousNumberOfLargeCells: numberOfLargeCells) |
|||
} |
|||
|
|||
open func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int) { |
|||
guard shouldUpdateButtonBarView else { return } |
|||
buttonBarView.moveTo(index: toIndex, animated: true, swipeDirection: toIndex < fromIndex ? .right : .left, pagerScroll: .yes) |
|||
|
|||
if let changeCurrentIndex = changeCurrentIndex { |
|||
let oldCell = buttonBarView.cellForItem(at: IndexPath(item: currentIndex != fromIndex ? fromIndex : toIndex, section: 0)) as? ButtonBarCellType |
|||
let newCell = buttonBarView.cellForItem(at: IndexPath(item: currentIndex, section: 0)) as? ButtonBarCellType |
|||
changeCurrentIndex(oldCell, newCell, true) |
|||
} |
|||
} |
|||
|
|||
open func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool) { |
|||
guard shouldUpdateButtonBarView else { return } |
|||
buttonBarView.move(fromIndex: fromIndex, toIndex: toIndex, progressPercentage: progressPercentage, pagerScroll: .yes) |
|||
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive { |
|||
let oldCell = buttonBarView.cellForItem(at: IndexPath(item: currentIndex != fromIndex ? fromIndex : toIndex, section: 0)) as? ButtonBarCellType |
|||
let newCell = buttonBarView.cellForItem(at: IndexPath(item: currentIndex, section: 0)) as? ButtonBarCellType |
|||
changeCurrentIndexProgressive(oldCell, newCell, progressPercentage, indexWasChanged, true) |
|||
} |
|||
} |
|||
|
|||
// MARK: - UICollectionViewDelegateFlowLayut |
|||
|
|||
@objc open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize { |
|||
guard let cellWidthValue = cachedCellWidths?[indexPath.row] else { |
|||
fatalError("cachedCellWidths for \(indexPath.row) must not be nil") |
|||
} |
|||
return CGSize(width: cellWidthValue, height: collectionView.frame.size.height) |
|||
} |
|||
|
|||
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { |
|||
guard indexPath.item != currentIndex else { return } |
|||
|
|||
buttonBarView.moveTo(index: indexPath.item, animated: true, swipeDirection: .none, pagerScroll: .yes) |
|||
shouldUpdateButtonBarView = false |
|||
|
|||
let oldCell = buttonBarView.cellForItem(at: IndexPath(item: currentIndex, section: 0)) as? ButtonBarCellType |
|||
let newCell = buttonBarView.cellForItem(at: IndexPath(item: indexPath.item, section: 0)) as? ButtonBarCellType |
|||
if pagerBehaviour.isProgressiveIndicator { |
|||
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive { |
|||
changeCurrentIndexProgressive(oldCell, newCell, 1, true, true) |
|||
} |
|||
} else { |
|||
if let changeCurrentIndex = changeCurrentIndex { |
|||
changeCurrentIndex(oldCell, newCell, true) |
|||
} |
|||
} |
|||
moveToViewController(at: indexPath.item) |
|||
} |
|||
|
|||
// MARK: - UICollectionViewDataSource |
|||
|
|||
open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { |
|||
return viewControllers.count |
|||
} |
|||
|
|||
open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { |
|||
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? ButtonBarCellType else { |
|||
fatalError("UICollectionViewCell should be or extend from ButtonBarViewCell") |
|||
} |
|||
let childController = viewControllers[indexPath.item] as! IndicatorInfoProvider // swiftlint:disable:this force_cast |
|||
let indicatorInfo = childController.indicatorInfo(for: self) |
|||
|
|||
configure(cell: cell, for: indicatorInfo) |
|||
|
|||
if pagerBehaviour.isProgressiveIndicator { |
|||
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive { |
|||
changeCurrentIndexProgressive(currentIndex == indexPath.item ? nil : cell, currentIndex == indexPath.item ? cell : nil, 1, true, false) |
|||
} |
|||
} else { |
|||
if let changeCurrentIndex = changeCurrentIndex { |
|||
changeCurrentIndex(currentIndex == indexPath.item ? nil : cell, currentIndex == indexPath.item ? cell : nil, false) |
|||
} |
|||
} |
|||
|
|||
return cell |
|||
} |
|||
|
|||
// MARK: - UIScrollViewDelegate |
|||
|
|||
open override func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) { |
|||
super.scrollViewDidEndScrollingAnimation(scrollView) |
|||
|
|||
guard scrollView == containerView else { return } |
|||
shouldUpdateButtonBarView = true |
|||
} |
|||
|
|||
open func configure(cell: ButtonBarCellType, for indicatorInfo: IndicatorInfo) { |
|||
fatalError("You must override this method to set up ButtonBarView cell accordingly") |
|||
} |
|||
|
|||
private func calculateWidths() -> [CGFloat] { |
|||
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout // swiftlint:disable:this force_cast |
|||
let numberOfCells = viewControllers.count |
|||
|
|||
var minimumCellWidths = [CGFloat]() |
|||
var collectionViewContentWidth: CGFloat = 0 |
|||
|
|||
for viewController in viewControllers { |
|||
let childController = viewController as! IndicatorInfoProvider // swiftlint:disable:this force_cast |
|||
let indicatorInfo = childController.indicatorInfo(for: self) |
|||
switch buttonBarItemSpec! { |
|||
case .cellClass(let widthCallback): |
|||
let width = widthCallback(indicatorInfo) |
|||
minimumCellWidths.append(width) |
|||
collectionViewContentWidth += width |
|||
case .nibFile(_, _, let widthCallback): |
|||
let width = widthCallback(indicatorInfo) |
|||
minimumCellWidths.append(width) |
|||
collectionViewContentWidth += width |
|||
} |
|||
} |
|||
|
|||
let cellSpacingTotal = CGFloat(numberOfCells - 1) * flowLayout.minimumLineSpacing |
|||
collectionViewContentWidth += cellSpacingTotal |
|||
|
|||
let collectionViewAvailableVisibleWidth = buttonBarView.frame.size.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right |
|||
|
|||
if !settings.style.buttonBarItemsShouldFillAvailableWidth || collectionViewAvailableVisibleWidth < collectionViewContentWidth { |
|||
return minimumCellWidths |
|||
} else { |
|||
let stretchedCellWidthIfAllEqual = (collectionViewAvailableVisibleWidth - cellSpacingTotal) / CGFloat(numberOfCells) |
|||
let generalMinimumCellWidth = calculateStretchedCellWidths(minimumCellWidths, suggestedStretchedCellWidth: stretchedCellWidthIfAllEqual, previousNumberOfLargeCells: 0) |
|||
var stretchedCellWidths = [CGFloat]() |
|||
|
|||
for minimumCellWidthValue in minimumCellWidths { |
|||
let cellWidth = (minimumCellWidthValue > generalMinimumCellWidth) ? minimumCellWidthValue : generalMinimumCellWidth |
|||
stretchedCellWidths.append(cellWidth) |
|||
} |
|||
|
|||
return stretchedCellWidths |
|||
} |
|||
} |
|||
|
|||
private var shouldUpdateButtonBarView = true |
|||
} |
|||
|
|||
open class ExampleBaseButtonBarPagerTabStripViewController: BaseButtonBarPagerTabStripViewController<ButtonBarViewCell> { |
|||
|
|||
public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { |
|||
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) |
|||
initialize() |
|||
} |
|||
|
|||
public required init?(coder aDecoder: NSCoder) { |
|||
super.init(coder: aDecoder) |
|||
initialize() |
|||
} |
|||
|
|||
open func initialize() { |
|||
var bundle = Bundle(for: ButtonBarViewCell.self) |
|||
if let resourcePath = bundle.path(forResource: "XLPagerTabStrip", ofType: "bundle") { |
|||
if let resourcesBundle = Bundle(path: resourcePath) { |
|||
bundle = resourcesBundle |
|||
} |
|||
} |
|||
|
|||
buttonBarItemSpec = .nibFile(nibName: "ButtonCell", bundle: bundle, width: { [weak self] (childItemInfo) -> CGFloat in |
|||
let label = UILabel() |
|||
label.translatesAutoresizingMaskIntoConstraints = false |
|||
label.font = self?.settings.style.buttonBarItemFont ?? label.font |
|||
label.text = childItemInfo.title |
|||
let labelSize = label.intrinsicContentSize |
|||
return labelSize.width + CGFloat(self?.settings.style.buttonBarItemLeftRightMargin ?? 8 * 2) |
|||
}) |
|||
} |
|||
|
|||
open override func configure(cell: ButtonBarViewCell, for indicatorInfo: IndicatorInfo) { |
|||
cell.label.text = indicatorInfo.title |
|||
cell.accessibilityLabel = indicatorInfo.accessibilityLabel |
|||
if let image = indicatorInfo.image { |
|||
cell.imageView.image = image |
|||
} |
|||
if let highlightedImage = indicatorInfo.highlightedImage { |
|||
cell.imageView.highlightedImage = highlightedImage |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,415 @@ |
|||
// ButtonBarPagerTabStripViewController.swift |
|||
// XLPagerTabStrip ( https://github.com/xmartlabs/XLPagerTabStrip ) |
|||
// |
|||
// Copyright (c) 2017 Xmartlabs ( http://xmartlabs.com ) |
|||
// |
|||
// |
|||
// Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
// of this software and associated documentation files (the "Software"), to deal |
|||
// in the Software without restriction, including without limitation the rights |
|||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
// copies of the Software, and to permit persons to whom the Software is |
|||
// furnished to do so, subject to the following conditions: |
|||
// |
|||
// The above copyright notice and this permission notice shall be included in |
|||
// all copies or substantial portions of the Software. |
|||
// |
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
// THE SOFTWARE. |
|||
|
|||
import Foundation |
|||
|
|||
public enum ButtonBarItemSpec<CellType: UICollectionViewCell> { |
|||
|
|||
case nibFile(nibName: String, bundle: Bundle?, width:((IndicatorInfo)-> CGFloat)) |
|||
case cellClass(width:((IndicatorInfo)-> CGFloat)) |
|||
|
|||
public var weight: ((IndicatorInfo) -> CGFloat) { |
|||
switch self { |
|||
case .cellClass(let widthCallback): |
|||
return widthCallback |
|||
case .nibFile(_, _, let widthCallback): |
|||
return widthCallback |
|||
} |
|||
} |
|||
} |
|||
|
|||
public struct ButtonBarPagerTabStripSettings { |
|||
|
|||
public struct Style { |
|||
public var buttonBarBackgroundColor: UIColor? |
|||
public var buttonBarMinimumInteritemSpacing: CGFloat? |
|||
public var buttonBarMinimumLineSpacing: CGFloat? |
|||
public var buttonBarLeftContentInset: CGFloat? |
|||
public var buttonBarRightContentInset: CGFloat? |
|||
|
|||
public var selectedBarBackgroundColor = UIColor.black |
|||
public var selectedBarHeight: CGFloat = 5 |
|||
public var selectedBarVerticalAlignment: SelectedBarVerticalAlignment = .bottom |
|||
|
|||
public var buttonBarItemBackgroundColor: UIColor? |
|||
public var buttonBarItemFont = UIFont.systemFont(ofSize: 18) |
|||
public var buttonBarItemLeftRightMargin: CGFloat = 8 |
|||
public var buttonBarItemTitleColor: UIColor? |
|||
@available(*, deprecated: 7.0.0) public var buttonBarItemsShouldFillAvailiableWidth: Bool { |
|||
set { |
|||
buttonBarItemsShouldFillAvailableWidth = newValue |
|||
} |
|||
get { |
|||
return buttonBarItemsShouldFillAvailableWidth |
|||
} |
|||
} |
|||
public var buttonBarItemsShouldFillAvailableWidth = true |
|||
// only used if button bar is created programaticaly and not using storyboards or nib files |
|||
public var buttonBarHeight: CGFloat? |
|||
} |
|||
|
|||
public var style = Style() |
|||
} |
|||
|
|||
open class ButtonBarPagerTabStripViewController: PagerTabStripViewController, PagerTabStripDataSource, PagerTabStripIsProgressiveDelegate, UICollectionViewDelegate, UICollectionViewDataSource { |
|||
|
|||
public var settings = ButtonBarPagerTabStripSettings() |
|||
|
|||
public var buttonBarItemSpec: ButtonBarItemSpec<ButtonBarViewCell>! |
|||
|
|||
public var changeCurrentIndex: ((_ oldCell: ButtonBarViewCell?, _ newCell: ButtonBarViewCell?, _ animated: Bool) -> Void)? |
|||
public var changeCurrentIndexProgressive: ((_ oldCell: ButtonBarViewCell?, _ newCell: ButtonBarViewCell?, _ progressPercentage: CGFloat, _ changeCurrentIndex: Bool, _ animated: Bool) -> Void)? |
|||
|
|||
@IBOutlet public weak var buttonBarView: ButtonBarView! |
|||
|
|||
lazy private var cachedCellWidths: [CGFloat]? = { [unowned self] in |
|||
return self.calculateWidths() |
|||
}() |
|||
|
|||
override public init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { |
|||
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) |
|||
delegate = self |
|||
datasource = self |
|||
} |
|||
|
|||
required public init?(coder aDecoder: NSCoder) { |
|||
super.init(coder: aDecoder) |
|||
delegate = self |
|||
datasource = self |
|||
} |
|||
|
|||
open override func viewDidLoad() { |
|||
super.viewDidLoad() |
|||
|
|||
var bundle = Bundle(for: ButtonBarViewCell.self) |
|||
if let resourcePath = bundle.path(forResource: "XLPagerTabStrip", ofType: "bundle") { |
|||
if let resourcesBundle = Bundle(path: resourcePath) { |
|||
bundle = resourcesBundle |
|||
} |
|||
} |
|||
|
|||
buttonBarItemSpec = .nibFile(nibName: "ButtonCell", bundle: bundle, width: { [weak self] (childItemInfo) -> CGFloat in |
|||
let label = UILabel() |
|||
label.translatesAutoresizingMaskIntoConstraints = false |
|||
label.font = self?.settings.style.buttonBarItemFont |
|||
label.text = childItemInfo.title |
|||
let labelSize = label.intrinsicContentSize |
|||
return labelSize.width + (self?.settings.style.buttonBarItemLeftRightMargin ?? 8) * 2 |
|||
}) |
|||
|
|||
let buttonBarViewAux = buttonBarView ?? { |
|||
let flowLayout = UICollectionViewFlowLayout() |
|||
flowLayout.scrollDirection = .horizontal |
|||
let buttonBarHeight = settings.style.buttonBarHeight ?? 44 |
|||
let buttonBar = ButtonBarView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: buttonBarHeight), collectionViewLayout: flowLayout) |
|||
buttonBar.backgroundColor = .orange |
|||
buttonBar.selectedBar.backgroundColor = .black |
|||
buttonBar.autoresizingMask = .flexibleWidth |
|||
var newContainerViewFrame = containerView.frame |
|||
newContainerViewFrame.origin.y = buttonBarHeight |
|||
newContainerViewFrame.size.height = containerView.frame.size.height - (buttonBarHeight - containerView.frame.origin.y) |
|||
containerView.frame = newContainerViewFrame |
|||
return buttonBar |
|||
}() |
|||
buttonBarView = buttonBarViewAux |
|||
|
|||
if buttonBarView.superview == nil { |
|||
view.addSubview(buttonBarView) |
|||
} |
|||
if buttonBarView.delegate == nil { |
|||
buttonBarView.delegate = self |
|||
} |
|||
if buttonBarView.dataSource == nil { |
|||
buttonBarView.dataSource = self |
|||
} |
|||
buttonBarView.scrollsToTop = false |
|||
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout // swiftlint:disable:this force_cast |
|||
flowLayout.scrollDirection = .horizontal |
|||
flowLayout.minimumInteritemSpacing = settings.style.buttonBarMinimumInteritemSpacing ?? flowLayout.minimumInteritemSpacing |
|||
flowLayout.minimumLineSpacing = settings.style.buttonBarMinimumLineSpacing ?? flowLayout.minimumLineSpacing |
|||
let sectionInset = flowLayout.sectionInset |
|||
flowLayout.sectionInset = UIEdgeInsets(top: sectionInset.top, left: settings.style.buttonBarLeftContentInset ?? sectionInset.left, bottom: sectionInset.bottom, right: settings.style.buttonBarRightContentInset ?? sectionInset.right) |
|||
|
|||
buttonBarView.showsHorizontalScrollIndicator = false |
|||
buttonBarView.backgroundColor = settings.style.buttonBarBackgroundColor ?? buttonBarView.backgroundColor |
|||
buttonBarView.selectedBar.backgroundColor = settings.style.selectedBarBackgroundColor |
|||
|
|||
buttonBarView.selectedBarHeight = settings.style.selectedBarHeight |
|||
buttonBarView.selectedBarVerticalAlignment = settings.style.selectedBarVerticalAlignment |
|||
|
|||
// register button bar item cell |
|||
switch buttonBarItemSpec! { |
|||
case .nibFile(let nibName, let bundle, _): |
|||
buttonBarView.register(UINib(nibName: nibName, bundle: bundle), forCellWithReuseIdentifier:"Cell") |
|||
case .cellClass: |
|||
buttonBarView.register(ButtonBarViewCell.self, forCellWithReuseIdentifier:"Cell") |
|||
} |
|||
//- |
|||
} |
|||
|
|||
open override func viewWillAppear(_ animated: Bool) { |
|||
super.viewWillAppear(animated) |
|||
buttonBarView.layoutIfNeeded() |
|||
} |
|||
|
|||
open override func viewDidLayoutSubviews() { |
|||
super.viewDidLayoutSubviews() |
|||
|
|||
guard isViewAppearing || isViewRotating else { return } |
|||
|
|||
// Force the UICollectionViewFlowLayout to get laid out again with the new size if |
|||
// a) The view is appearing. This ensures that |
|||
// collectionView:layout:sizeForItemAtIndexPath: is called for a second time |
|||
// when the view is shown and when the view *frame(s)* are actually set |
|||
// (we need the view frame's to have been set to work out the size's and on the |
|||
// first call to collectionView:layout:sizeForItemAtIndexPath: the view frame(s) |
|||
// aren't set correctly) |
|||
// b) The view is rotating. This ensures that |
|||
// collectionView:layout:sizeForItemAtIndexPath: is called again and can use the views |
|||
// *new* frame so that the buttonBarView cell's actually get resized correctly |
|||
cachedCellWidths = calculateWidths() |
|||
buttonBarView.collectionViewLayout.invalidateLayout() |
|||
// When the view first appears or is rotated we also need to ensure that the barButtonView's |
|||
// selectedBar is resized and its contentOffset/scroll is set correctly (the selected |
|||
// tab/cell may end up either skewed or off screen after a rotation otherwise) |
|||
buttonBarView.moveTo(index: currentIndex, animated: false, swipeDirection: .none, pagerScroll: .scrollOnlyIfOutOfScreen) |
|||
buttonBarView.selectItem(at: IndexPath(item: currentIndex, section: 0), animated: false, scrollPosition: []) |
|||
} |
|||
|
|||
// MARK: - Public Methods |
|||
|
|||
open override func reloadPagerTabStripView() { |
|||
super.reloadPagerTabStripView() |
|||
guard isViewLoaded else { return } |
|||
buttonBarView.reloadData() |
|||
cachedCellWidths = calculateWidths() |
|||
buttonBarView.moveTo(index: currentIndex, animated: false, swipeDirection: .none, pagerScroll: .yes) |
|||
} |
|||
|
|||
open func calculateStretchedCellWidths(_ minimumCellWidths: [CGFloat], suggestedStretchedCellWidth: CGFloat, previousNumberOfLargeCells: Int) -> CGFloat { |
|||
var numberOfLargeCells = 0 |
|||
var totalWidthOfLargeCells: CGFloat = 0 |
|||
|
|||
for minimumCellWidthValue in minimumCellWidths where minimumCellWidthValue > suggestedStretchedCellWidth { |
|||
totalWidthOfLargeCells += minimumCellWidthValue |
|||
numberOfLargeCells += 1 |
|||
} |
|||
|
|||
guard numberOfLargeCells > previousNumberOfLargeCells else { return suggestedStretchedCellWidth } |
|||
|
|||
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout // swiftlint:disable:this force_cast |
|||
let collectionViewAvailiableWidth = buttonBarView.frame.size.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right |
|||
let numberOfCells = minimumCellWidths.count |
|||
let cellSpacingTotal = CGFloat(numberOfCells - 1) * flowLayout.minimumLineSpacing |
|||
|
|||
let numberOfSmallCells = numberOfCells - numberOfLargeCells |
|||
let newSuggestedStretchedCellWidth = (collectionViewAvailiableWidth - totalWidthOfLargeCells - cellSpacingTotal) / CGFloat(numberOfSmallCells) |
|||
|
|||
return calculateStretchedCellWidths(minimumCellWidths, suggestedStretchedCellWidth: newSuggestedStretchedCellWidth, previousNumberOfLargeCells: numberOfLargeCells) |
|||
} |
|||
|
|||
open func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int) { |
|||
guard shouldUpdateButtonBarView else { return } |
|||
buttonBarView.moveTo(index: toIndex, animated: false, swipeDirection: toIndex < fromIndex ? .right : .left, pagerScroll: .yes) |
|||
|
|||
if let changeCurrentIndex = changeCurrentIndex { |
|||
let oldIndexPath = IndexPath(item: currentIndex != fromIndex ? fromIndex : toIndex, section: 0) |
|||
let newIndexPath = IndexPath(item: currentIndex, section: 0) |
|||
|
|||
let cells = cellForItems(at: [oldIndexPath, newIndexPath], reloadIfNotVisible: collectionViewDidLoad) |
|||
changeCurrentIndex(cells.first!, cells.last!, true) |
|||
} |
|||
} |
|||
|
|||
open func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool) { |
|||
guard shouldUpdateButtonBarView else { return } |
|||
buttonBarView.move(fromIndex: fromIndex, toIndex: toIndex, progressPercentage: progressPercentage, pagerScroll: .yes) |
|||
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive { |
|||
let oldIndexPath = IndexPath(item: currentIndex != fromIndex ? fromIndex : toIndex, section: 0) |
|||
let newIndexPath = IndexPath(item: currentIndex, section: 0) |
|||
|
|||
let cells = cellForItems(at: [oldIndexPath, newIndexPath], reloadIfNotVisible: collectionViewDidLoad) |
|||
changeCurrentIndexProgressive(cells.first!, cells.last!, progressPercentage, indexWasChanged, true) |
|||
} |
|||
} |
|||
|
|||
private func cellForItems(at indexPaths: [IndexPath], reloadIfNotVisible reload: Bool = true) -> [ButtonBarViewCell?] { |
|||
let cells = indexPaths.map { buttonBarView.cellForItem(at: $0) as? ButtonBarViewCell } |
|||
|
|||
if reload { |
|||
let indexPathsToReload = cells.enumerated() |
|||
.flatMap { (arg) -> IndexPath? in |
|||
let (index, cell) = arg |
|||
return cell == nil ? indexPaths[index] : nil |
|||
} |
|||
.flatMap { (indexPath: IndexPath) -> IndexPath? in |
|||
return (indexPath.item >= 0 && indexPath.item < buttonBarView.numberOfItems(inSection: indexPath.section)) ? indexPath : nil |
|||
} |
|||
|
|||
if !indexPathsToReload.isEmpty { |
|||
buttonBarView.reloadItems(at: indexPathsToReload) |
|||
} |
|||
} |
|||
|
|||
return cells |
|||
} |
|||
|
|||
// MARK: - UICollectionViewDelegateFlowLayut |
|||
|
|||
@objc open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize { |
|||
guard let cellWidthValue = cachedCellWidths?[indexPath.row] else { |
|||
fatalError("cachedCellWidths for \(indexPath.row) must not be nil") |
|||
} |
|||
return CGSize(width: cellWidthValue, height: collectionView.frame.size.height) |
|||
} |
|||
|
|||
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { |
|||
guard indexPath.item != currentIndex else { return } |
|||
|
|||
buttonBarView.moveTo(index: indexPath.item, animated: true, swipeDirection: .none, pagerScroll: .yes) |
|||
shouldUpdateButtonBarView = false |
|||
|
|||
let oldIndexPath = IndexPath(item: currentIndex, section: 0) |
|||
let newIndexPath = IndexPath(item: indexPath.item, section: 0) |
|||
|
|||
let cells = cellForItems(at: [oldIndexPath, newIndexPath], reloadIfNotVisible: collectionViewDidLoad) |
|||
|
|||
if pagerBehaviour.isProgressiveIndicator { |
|||
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive { |
|||
changeCurrentIndexProgressive(cells.first!, cells.last!, 1, true, true) |
|||
} |
|||
} else { |
|||
if let changeCurrentIndex = changeCurrentIndex { |
|||
changeCurrentIndex(cells.first!, cells.last!, true) |
|||
} |
|||
} |
|||
moveToViewController(at: indexPath.item) |
|||
} |
|||
|
|||
// MARK: - UICollectionViewDataSource |
|||
|
|||
open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { |
|||
return viewControllers.count |
|||
} |
|||
|
|||
open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { |
|||
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? ButtonBarViewCell else { |
|||
fatalError("UICollectionViewCell should be or extend from ButtonBarViewCell") |
|||
} |
|||
|
|||
collectionViewDidLoad = true |
|||
|
|||
let childController = viewControllers[indexPath.item] as! IndicatorInfoProvider // swiftlint:disable:this force_cast |
|||
let indicatorInfo = childController.indicatorInfo(for: self) |
|||
|
|||
cell.label.text = indicatorInfo.title |
|||
cell.accessibilityLabel = indicatorInfo.accessibilityLabel |
|||
cell.label.font = settings.style.buttonBarItemFont |
|||
cell.label.textColor = settings.style.buttonBarItemTitleColor ?? cell.label.textColor |
|||
cell.contentView.backgroundColor = settings.style.buttonBarItemBackgroundColor ?? cell.contentView.backgroundColor |
|||
cell.backgroundColor = settings.style.buttonBarItemBackgroundColor ?? cell.backgroundColor |
|||
if let image = indicatorInfo.image { |
|||
cell.imageView.image = image |
|||
} |
|||
if let highlightedImage = indicatorInfo.highlightedImage { |
|||
cell.imageView.highlightedImage = highlightedImage |
|||
} |
|||
|
|||
configureCell(cell, indicatorInfo: indicatorInfo) |
|||
|
|||
if pagerBehaviour.isProgressiveIndicator { |
|||
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive { |
|||
changeCurrentIndexProgressive(currentIndex == indexPath.item ? nil : cell, currentIndex == indexPath.item ? cell : nil, 1, true, false) |
|||
} |
|||
} else { |
|||
if let changeCurrentIndex = changeCurrentIndex { |
|||
changeCurrentIndex(currentIndex == indexPath.item ? nil : cell, currentIndex == indexPath.item ? cell : nil, false) |
|||
} |
|||
} |
|||
cell.isAccessibilityElement = true |
|||
cell.accessibilityLabel = cell.label.text |
|||
cell.accessibilityTraits |= UIAccessibilityTraitButton |
|||
cell.accessibilityTraits |= UIAccessibilityTraitHeader |
|||
return cell |
|||
} |
|||
|
|||
// MARK: - UIScrollViewDelegate |
|||
|
|||
open override func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) { |
|||
super.scrollViewDidEndScrollingAnimation(scrollView) |
|||
|
|||
guard scrollView == containerView else { return } |
|||
shouldUpdateButtonBarView = true |
|||
} |
|||
|
|||
open func configureCell(_ cell: ButtonBarViewCell, indicatorInfo: IndicatorInfo) { |
|||
} |
|||
|
|||
private func calculateWidths() -> [CGFloat] { |
|||
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout // swiftlint:disable:this force_cast |
|||
let numberOfCells = viewControllers.count |
|||
|
|||
var minimumCellWidths = [CGFloat]() |
|||
var collectionViewContentWidth: CGFloat = 0 |
|||
|
|||
for viewController in viewControllers { |
|||
let childController = viewController as! IndicatorInfoProvider // swiftlint:disable:this force_cast |
|||
let indicatorInfo = childController.indicatorInfo(for: self) |
|||
switch buttonBarItemSpec! { |
|||
case .cellClass(let widthCallback): |
|||
let width = widthCallback(indicatorInfo) |
|||
minimumCellWidths.append(width) |
|||
collectionViewContentWidth += width |
|||
case .nibFile(_, _, let widthCallback): |
|||
let width = widthCallback(indicatorInfo) |
|||
minimumCellWidths.append(width) |
|||
collectionViewContentWidth += width |
|||
} |
|||
} |
|||
|
|||
let cellSpacingTotal = CGFloat(numberOfCells - 1) * flowLayout.minimumLineSpacing |
|||
collectionViewContentWidth += cellSpacingTotal |
|||
|
|||
let collectionViewAvailableVisibleWidth = buttonBarView.frame.size.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right |
|||
|
|||
if !settings.style.buttonBarItemsShouldFillAvailableWidth || collectionViewAvailableVisibleWidth < collectionViewContentWidth { |
|||
return minimumCellWidths |
|||
} else { |
|||
let stretchedCellWidthIfAllEqual = (collectionViewAvailableVisibleWidth - cellSpacingTotal) / CGFloat(numberOfCells) |
|||
let generalMinimumCellWidth = calculateStretchedCellWidths(minimumCellWidths, suggestedStretchedCellWidth: stretchedCellWidthIfAllEqual, previousNumberOfLargeCells: 0) |
|||
var stretchedCellWidths = [CGFloat]() |
|||
|
|||
for minimumCellWidthValue in minimumCellWidths { |
|||
let cellWidth = (minimumCellWidthValue > generalMinimumCellWidth) ? minimumCellWidthValue : generalMinimumCellWidth |
|||
stretchedCellWidths.append(cellWidth) |
|||
} |
|||
|
|||
return stretchedCellWidths |
|||
} |
|||
} |
|||
|
|||
private var shouldUpdateButtonBarView = true |
|||
private var collectionViewDidLoad = false |
|||
|
|||
} |
@ -0,0 +1,191 @@ |
|||
// ButtonBarView.swift |
|||
// XLPagerTabStrip ( https://github.com/xmartlabs/XLPagerTabStrip ) |
|||
// |
|||
// Copyright (c) 2017 Xmartlabs ( http://xmartlabs.com ) |
|||
// |
|||
// |
|||
// Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
// of this software and associated documentation files (the "Software"), to deal |
|||
// in the Software without restriction, including without limitation the rights |
|||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
// copies of the Software, and to permit persons to whom the Software is |
|||
// furnished to do so, subject to the following conditions: |
|||
// |
|||
// The above copyright notice and this permission notice shall be included in |
|||
// all copies or substantial portions of the Software. |
|||
// |
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
// THE SOFTWARE. |
|||
|
|||
import UIKit |
|||
|
|||
public enum PagerScroll { |
|||
case no |
|||
case yes |
|||
case scrollOnlyIfOutOfScreen |
|||
} |
|||
|
|||
public enum SelectedBarAlignment { |
|||
case left |
|||
case center |
|||
case right |
|||
case progressive |
|||
} |
|||
|
|||
public enum SelectedBarVerticalAlignment { |
|||
case top |
|||
case middle |
|||
case bottom |
|||
} |
|||
|
|||
open class ButtonBarView: UICollectionView { |
|||
|
|||
open lazy var selectedBar: UIView = { [unowned self] in |
|||
let bar = UIView(frame: CGRect(x: 0, y: self.frame.size.height - CGFloat(self.selectedBarHeight), width: 0, height: CGFloat(self.selectedBarHeight))) |
|||
bar.layer.zPosition = 9999 |
|||
return bar |
|||
}() |
|||
|
|||
internal var selectedBarHeight: CGFloat = 4 { |
|||
didSet { |
|||
updateSelectedBarYPosition() |
|||
} |
|||
} |
|||
var selectedBarVerticalAlignment: SelectedBarVerticalAlignment = .bottom |
|||
var selectedBarAlignment: SelectedBarAlignment = .center |
|||
var selectedIndex = 0 |
|||
|
|||
required public init?(coder aDecoder: NSCoder) { |
|||
super.init(coder: aDecoder) |
|||
addSubview(selectedBar) |
|||
} |
|||
|
|||
public override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) { |
|||
super.init(frame: frame, collectionViewLayout: layout) |
|||
addSubview(selectedBar) |
|||
} |
|||
|
|||
open func moveTo(index: Int, animated: Bool, swipeDirection: SwipeDirection, pagerScroll: PagerScroll) { |
|||
selectedIndex = index |
|||
updateSelectedBarPosition(animated, swipeDirection: swipeDirection, pagerScroll: pagerScroll) |
|||
} |
|||
|
|||
open func move(fromIndex: Int, toIndex: Int, progressPercentage: CGFloat, pagerScroll: PagerScroll) { |
|||
selectedIndex = progressPercentage > 0.5 ? toIndex : fromIndex |
|||
|
|||
let fromFrame = layoutAttributesForItem(at: IndexPath(item: fromIndex, section: 0))!.frame |
|||
let numberOfItems = dataSource!.collectionView(self, numberOfItemsInSection: 0) |
|||
|
|||
var toFrame: CGRect |
|||
|
|||
if toIndex < 0 || toIndex > numberOfItems - 1 { |
|||
if toIndex < 0 { |
|||
let cellAtts = layoutAttributesForItem(at: IndexPath(item: 0, section: 0)) |
|||
toFrame = cellAtts!.frame.offsetBy(dx: -cellAtts!.frame.size.width, dy: 0) |
|||
} else { |
|||
let cellAtts = layoutAttributesForItem(at: IndexPath(item: (numberOfItems - 1), section: 0)) |
|||
toFrame = cellAtts!.frame.offsetBy(dx: cellAtts!.frame.size.width, dy: 0) |
|||
} |
|||
} else { |
|||
toFrame = layoutAttributesForItem(at: IndexPath(item: toIndex, section: 0))!.frame |
|||
} |
|||
|
|||
var targetFrame = fromFrame |
|||
targetFrame.size.height = selectedBar.frame.size.height |
|||
targetFrame.size.width += (toFrame.size.width - fromFrame.size.width) * progressPercentage |
|||
targetFrame.origin.x += (toFrame.origin.x - fromFrame.origin.x) * progressPercentage |
|||
|
|||
selectedBar.frame = CGRect(x: targetFrame.origin.x, y: selectedBar.frame.origin.y, width: targetFrame.size.width, height: selectedBar.frame.size.height) |
|||
|
|||
var targetContentOffset: CGFloat = 0.0 |
|||
if contentSize.width > frame.size.width { |
|||
let toContentOffset = contentOffsetForCell(withFrame: toFrame, andIndex: toIndex) |
|||
let fromContentOffset = contentOffsetForCell(withFrame: fromFrame, andIndex: fromIndex) |
|||
|
|||
targetContentOffset = fromContentOffset + ((toContentOffset - fromContentOffset) * progressPercentage) |
|||
} |
|||
|
|||
setContentOffset(CGPoint(x: targetContentOffset, y: 0), animated: false) |
|||
} |
|||
|
|||
open func updateSelectedBarPosition(_ animated: Bool, swipeDirection: SwipeDirection, pagerScroll: PagerScroll) { |
|||
var selectedBarFrame = selectedBar.frame |
|||
|
|||
let selectedCellIndexPath = IndexPath(item: selectedIndex, section: 0) |
|||
let attributes = layoutAttributesForItem(at: selectedCellIndexPath) |
|||
let selectedCellFrame = attributes!.frame |
|||
|
|||
updateContentOffset(animated: animated, pagerScroll: pagerScroll, toFrame: selectedCellFrame, toIndex: (selectedCellIndexPath as NSIndexPath).row) |
|||
|
|||
selectedBarFrame.size.width = selectedCellFrame.size.width |
|||
selectedBarFrame.origin.x = selectedCellFrame.origin.x |
|||
|
|||
if animated { |
|||
UIView.animate(withDuration: 0.3, animations: { [weak self] in |
|||
self?.selectedBar.frame = selectedBarFrame |
|||
}) |
|||
} else { |
|||
selectedBar.frame = selectedBarFrame |
|||
} |
|||
} |
|||
|
|||
// MARK: - Helpers |
|||
|
|||
private func updateContentOffset(animated: Bool, pagerScroll: PagerScroll, toFrame: CGRect, toIndex: Int) { |
|||
guard pagerScroll != .no || (pagerScroll != .scrollOnlyIfOutOfScreen && (toFrame.origin.x < contentOffset.x || toFrame.origin.x >= (contentOffset.x + frame.size.width - contentInset.left))) else { return } |
|||
let targetContentOffset = contentSize.width > frame.size.width ? contentOffsetForCell(withFrame: toFrame, andIndex: toIndex) : 0 |
|||
setContentOffset(CGPoint(x: targetContentOffset, y: 0), animated: animated) |
|||
} |
|||
|
|||
private func contentOffsetForCell(withFrame cellFrame: CGRect, andIndex index: Int) -> CGFloat { |
|||
let sectionInset = (collectionViewLayout as! UICollectionViewFlowLayout).sectionInset // swiftlint:disable:this force_cast |
|||
var alignmentOffset: CGFloat = 0.0 |
|||
|
|||
switch selectedBarAlignment { |
|||
case .left: |
|||
alignmentOffset = sectionInset.left |
|||
case .right: |
|||
alignmentOffset = frame.size.width - sectionInset.right - cellFrame.size.width |
|||
case .center: |
|||
alignmentOffset = (frame.size.width - cellFrame.size.width) * 0.5 |
|||
case .progressive: |
|||
let cellHalfWidth = cellFrame.size.width * 0.5 |
|||
let leftAlignmentOffset = sectionInset.left + cellHalfWidth |
|||
let rightAlignmentOffset = frame.size.width - sectionInset.right - cellHalfWidth |
|||
let numberOfItems = dataSource!.collectionView(self, numberOfItemsInSection: 0) |
|||
let progress = index / (numberOfItems - 1) |
|||
alignmentOffset = leftAlignmentOffset + (rightAlignmentOffset - leftAlignmentOffset) * CGFloat(progress) - cellHalfWidth |
|||
} |
|||
|
|||
var contentOffset = cellFrame.origin.x - alignmentOffset |
|||
contentOffset = max(0, contentOffset) |
|||
contentOffset = min(contentSize.width - frame.size.width, contentOffset) |
|||
return contentOffset |
|||
} |
|||
|
|||
private func updateSelectedBarYPosition() { |
|||
var selectedBarFrame = selectedBar.frame |
|||
|
|||
switch selectedBarVerticalAlignment { |
|||
case .top: |
|||
selectedBarFrame.origin.y = 0 |
|||
case .middle: |
|||
selectedBarFrame.origin.y = (frame.size.height - selectedBarHeight) / 2 |
|||
case .bottom: |
|||
selectedBarFrame.origin.y = frame.size.height - selectedBarHeight |
|||
} |
|||
|
|||
selectedBarFrame.size.height = selectedBarHeight |
|||
selectedBar.frame = selectedBarFrame |
|||
} |
|||
|
|||
override open func layoutSubviews() { |
|||
super.layoutSubviews() |
|||
updateSelectedBarYPosition() |
|||
} |
|||
} |
@ -0,0 +1,53 @@ |
|||
// ButtonBarViewCell.swift |
|||
// XLPagerTabStrip ( https://github.com/xmartlabs/XLPagerTabStrip ) |
|||
// |
|||
// Copyright (c) 2017 Xmartlabs ( http://xmartlabs.com ) |
|||
// |
|||
// |
|||
// Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
// of this software and associated documentation files (the "Software"), to deal |
|||
// in the Software without restriction, including without limitation the rights |
|||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
// copies of the Software, and to permit persons to whom the Software is |
|||
// furnished to do so, subject to the following conditions: |
|||
// |
|||
// The above copyright notice and this permission notice shall be included in |
|||
// all copies or substantial portions of the Software. |
|||
// |
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
// THE SOFTWARE. |
|||
|
|||
import Foundation |
|||
|
|||
open class ButtonBarViewCell: UICollectionViewCell { |
|||
|
|||
@IBOutlet open var imageView: UIImageView! |
|||
@IBOutlet open var label: UILabel! |
|||
|
|||
public required init?(coder aDecoder: NSCoder) { |
|||
super.init(coder: aDecoder) |
|||
|
|||
isAccessibilityElement = true |
|||
accessibilityTraits |= UIAccessibilityTraitButton |
|||
accessibilityTraits |= UIAccessibilityTraitHeader |
|||
} |
|||
|
|||
open override var isSelected: Bool { |
|||
get { |
|||
return super.isSelected |
|||
} |
|||
set { |
|||
super.isSelected = newValue |
|||
if (newValue) { |
|||
accessibilityTraits |= UIAccessibilityTraitSelected |
|||
} else { |
|||
accessibilityTraits &= ~UIAccessibilityTraitSelected |
|||
} |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,50 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES"> |
|||
<device id="retina4_7" orientation="portrait"> |
|||
<adaptation id="fullscreen"/> |
|||
</device> |
|||
<dependencies> |
|||
<deployment identifier="iOS"/> |
|||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/> |
|||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> |
|||
</dependencies> |
|||
<objects> |
|||
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/> |
|||
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/> |
|||
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" id="zg4-fX-zUF" customClass="ButtonBarViewCell" customModule="XLPagerTabStrip" customModuleProvider="target"> |
|||
<rect key="frame" x="0.0" y="0.0" width="80" height="40"/> |
|||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> |
|||
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center"> |
|||
<rect key="frame" x="0.0" y="0.0" width="80" height="40"/> |
|||
<autoresizingMask key="autoresizingMask"/> |
|||
<subviews> |
|||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="s7f-rk-sQl"> |
|||
<rect key="frame" x="22" y="12" width="37.5" height="17"/> |
|||
<fontDescription key="fontDescription" type="boldSystem" pointSize="14"/> |
|||
<color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> |
|||
<nil key="highlightedColor"/> |
|||
</label> |
|||
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="tFG-yJ-6Th"> |
|||
<rect key="frame" x="22.5" y="2.5" width="35" height="35"/> |
|||
<constraints> |
|||
<constraint firstAttribute="width" constant="35" id="VRw-5F-5WY"/> |
|||
<constraint firstAttribute="height" constant="35" id="ojk-Ug-Lgh"/> |
|||
</constraints> |
|||
</imageView> |
|||
</subviews> |
|||
</view> |
|||
<color key="backgroundColor" red="0.027450980390000001" green="0.72549019609999998" blue="0.60784313729999995" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> |
|||
<constraints> |
|||
<constraint firstItem="tFG-yJ-6Th" firstAttribute="centerY" secondItem="zg4-fX-zUF" secondAttribute="centerY" id="5so-ZP-gRs"/> |
|||
<constraint firstItem="tFG-yJ-6Th" firstAttribute="centerX" secondItem="zg4-fX-zUF" secondAttribute="centerX" id="9em-NR-hoa"/> |
|||
<constraint firstItem="s7f-rk-sQl" firstAttribute="centerY" secondItem="zg4-fX-zUF" secondAttribute="centerY" id="ZeP-6I-AXE"/> |
|||
<constraint firstItem="s7f-rk-sQl" firstAttribute="centerX" secondItem="zg4-fX-zUF" secondAttribute="centerX" id="c15-bZ-hPG"/> |
|||
</constraints> |
|||
<connections> |
|||
<outlet property="imageView" destination="tFG-yJ-6Th" id="Odb-dR-tf4"/> |
|||
<outlet property="label" destination="s7f-rk-sQl" id="4gU-tb-BMB"/> |
|||
</connections> |
|||
<point key="canvasLocation" x="307" y="541"/> |
|||
</collectionViewCell> |
|||
</objects> |
|||
</document> |
@ -0,0 +1,106 @@ |
|||
// |
|||
// FXPageControl.h |
|||
// |
|||
// Version 1.4 |
|||
// |
|||
// Created by Nick Lockwood on 07/01/2010. |
|||
// Copyright 2010 Charcoal Design |
|||
// |
|||
// Distributed under the permissive zlib License |
|||
// Get the latest version of FXPageControl from here: |
|||
// |
|||
// https://github.com/nicklockwood/FXPageControl |
|||
// |
|||
// This software is provided 'as-is', without any express or implied |
|||
// warranty. In no event will the authors be held liable for any damages |
|||
// arising from the use of this software. |
|||
// |
|||
// Permission is granted to anyone to use this software for any purpose, |
|||
// including commercial applications, and to alter it and redistribute it |
|||
// freely, subject to the following restrictions: |
|||
// |
|||
// 1. The origin of this software must not be misrepresented; you must not |
|||
// claim that you wrote the original software. If you use this software |
|||
// in a product, an acknowledgment in the product documentation would be |
|||
// appreciated but is not required. |
|||
// |
|||
// 2. Altered source versions must be plainly marked as such, and must not be |
|||
// misrepresented as being the original software. |
|||
// |
|||
// 3. This notice may not be removed or altered from any source distribution. |
|||
// |
|||
|
|||
|
|||
#pragma GCC diagnostic push |
|||
#pragma GCC diagnostic ignored "-Wobjc-missing-property-synthesis" |
|||
#import <UIKit/UIKit.h> |
|||
|
|||
|
|||
#import <Availability.h> |
|||
#undef weak_delegate |
|||
#if __has_feature(objc_arc_weak) |
|||
#define weak_delegate weak |
|||
#else |
|||
#define weak_delegate unsafe_unretained |
|||
#endif |
|||
|
|||
|
|||
extern const CGPathRef FXPageControlDotShapeCircle; |
|||
extern const CGPathRef FXPageControlDotShapeSquare; |
|||
extern const CGPathRef FXPageControlDotShapeTriangle; |
|||
|
|||
|
|||
@protocol FXPageControlDelegate; |
|||
|
|||
|
|||
IB_DESIGNABLE @interface FXPageControl : UIControl |
|||
|
|||
- (void)setUp; |
|||
- (CGSize)sizeForNumberOfPages:(NSInteger)pageCount; |
|||
- (void)updateCurrentPageDisplay; |
|||
|
|||
@property (nonatomic, weak_delegate) IBOutlet id <FXPageControlDelegate> delegate; |
|||
|
|||
@property (nonatomic, assign) IBInspectable NSInteger currentPage; |
|||
@property (nonatomic, assign) IBInspectable NSInteger numberOfPages; |
|||
@property (nonatomic, assign) IBInspectable BOOL defersCurrentPageDisplay; |
|||
@property (nonatomic, assign) IBInspectable BOOL hidesForSinglePage; |
|||
@property (nonatomic, assign, getter = isWrapEnabled) IBInspectable BOOL wrapEnabled; |
|||
@property (nonatomic, assign, getter = isVertical) IBInspectable BOOL vertical; |
|||
|
|||
@property (nonatomic, strong) IBInspectable UIImage *dotImage; |
|||
@property (nonatomic, assign) IBInspectable CGPathRef dotShape; |
|||
@property (nonatomic, assign) IBInspectable CGFloat dotSize; |
|||
@property (nonatomic, strong) IBInspectable UIColor *dotColor; |
|||
@property (nonatomic, strong) IBInspectable UIColor *dotShadowColor; |
|||
@property (nonatomic, assign) IBInspectable CGFloat dotShadowBlur; |
|||
@property (nonatomic, assign) IBInspectable CGSize dotShadowOffset; |
|||
|
|||
@property (nonatomic, strong) IBInspectable UIImage *selectedDotImage; |
|||
@property (nonatomic, assign) IBInspectable CGPathRef selectedDotShape; |
|||
@property (nonatomic, assign) IBInspectable CGFloat selectedDotSize; |
|||
@property (nonatomic, strong) IBInspectable UIColor *selectedDotColor; |
|||
@property (nonatomic, strong) IBInspectable UIColor *selectedDotShadowColor; |
|||
@property (nonatomic, assign) IBInspectable CGFloat selectedDotShadowBlur; |
|||
@property (nonatomic, assign) IBInspectable CGSize selectedDotShadowOffset; |
|||
|
|||
@property (nonatomic, assign) IBInspectable CGFloat dotSpacing; |
|||
|
|||
@end |
|||
|
|||
|
|||
@protocol FXPageControlDelegate <NSObject> |
|||
@optional |
|||
|
|||
- (UIImage *)pageControl:(FXPageControl *)pageControl imageForDotAtIndex:(NSInteger)index; |
|||
- (CGPathRef)pageControl:(FXPageControl *)pageControl shapeForDotAtIndex:(NSInteger)index; |
|||
- (UIColor *)pageControl:(FXPageControl *)pageControl colorForDotAtIndex:(NSInteger)index; |
|||
|
|||
- (UIImage *)pageControl:(FXPageControl *)pageControl selectedImageForDotAtIndex:(NSInteger)index; |
|||
- (CGPathRef)pageControl:(FXPageControl *)pageControl selectedShapeForDotAtIndex:(NSInteger)index; |
|||
- (UIColor *)pageControl:(FXPageControl *)pageControl selectedColorForDotAtIndex:(NSInteger)index; |
|||
|
|||
@end |
|||
|
|||
|
|||
#pragma GCC diagnostic pop |
@ -0,0 +1,432 @@ |
|||
// |
|||
// FXPageControl.m |
|||
// |
|||
// Version 1.4 |
|||
// |
|||
// Created by Nick Lockwood on 07/01/2010. |
|||
// Copyright 2010 Charcoal Design |
|||
// |
|||
// Distributed under the permissive zlib License |
|||
// Get the latest version of FXPageControl from here: |
|||
// |
|||
// https://github.com/nicklockwood/FXPageControl |
|||
// |
|||
// This software is provided 'as-is', without any express or implied |
|||
// warranty. In no event will the authors be held liable for any damages |
|||
// arising from the use of this software. |
|||
// |
|||
// Permission is granted to anyone to use this software for any purpose, |
|||
// including commercial applications, and to alter it and redistribute it |
|||
// freely, subject to the following restrictions: |
|||
// |
|||
// 1. The origin of this software must not be misrepresented; you must not |
|||
// claim that you wrote the original software. If you use this software |
|||
// in a product, an acknowledgment in the product documentation would be |
|||
// appreciated but is not required. |
|||
// |
|||
// 2. Altered source versions must be plainly marked as such, and must not be |
|||
// misrepresented as being the original software. |
|||
// |
|||
// 3. This notice may not be removed or altered from any source distribution. |
|||
// |
|||
|
|||
#import "FXPageControl.h" |
|||
|
|||
|
|||
#pragma GCC diagnostic ignored "-Wgnu" |
|||
#pragma GCC diagnostic ignored "-Warc-repeated-use-of-weak" |
|||
#pragma GCC diagnostic ignored "-Wdirect-ivar-access" |
|||
|
|||
|
|||
#import <Availability.h> |
|||
#if !__has_feature(objc_arc) |
|||
#error This class requires automatic reference counting |
|||
#endif |
|||
|
|||
|
|||
const CGPathRef FXPageControlDotShapeCircle = (const CGPathRef)1; |
|||
const CGPathRef FXPageControlDotShapeSquare = (const CGPathRef)2; |
|||
const CGPathRef FXPageControlDotShapeTriangle = (const CGPathRef)3; |
|||
#define LAST_SHAPE FXPageControlDotShapeTriangle |
|||
|
|||
|
|||
@implementation NSObject (FXPageControl) |
|||
|
|||
- (UIImage *)pageControl:(__unused FXPageControl *)pageControl imageForDotAtIndex:(__unused NSInteger)index { return nil; } |
|||
- (CGPathRef)pageControl:(__unused FXPageControl *)pageControl shapeForDotAtIndex:(__unused NSInteger)index { return NULL; } |
|||
- (UIColor *)pageControl:(__unused FXPageControl *)pageControl colorForDotAtIndex:(__unused NSInteger)index { return nil; } |
|||
|
|||
- (UIImage *)pageControl:(__unused FXPageControl *)pageControl selectedImageForDotAtIndex:(__unused NSInteger)index { return nil; } |
|||
- (CGPathRef)pageControl:(__unused FXPageControl *)pageControl selectedShapeForDotAtIndex:(__unused NSInteger)index { return NULL; } |
|||
- (UIColor *)pageControl:(__unused FXPageControl *)pageControl selectedColorForDotAtIndex:(__unused NSInteger)index { return nil; } |
|||
|
|||
@end |
|||
|
|||
|
|||
@implementation FXPageControl |
|||
|
|||
- (void)setUp |
|||
{ |
|||
//needs redrawing if bounds change |
|||
self.contentMode = UIViewContentModeRedraw; |
|||
|
|||
//set defaults |
|||
_dotSpacing = 10.0f; |
|||
_dotSize = 6.0f; |
|||
_dotShadowOffset = CGSizeMake(0, 1); |
|||
_selectedDotShadowOffset = CGSizeMake(0, 1); |
|||
} |
|||
|
|||
- (id)initWithFrame:(CGRect)frame |
|||
{ |
|||
if ((self = [super initWithFrame:frame])) |
|||
{ |
|||
[self setUp]; |
|||
} |
|||
return self; |
|||
} |
|||
|
|||
- (id)initWithCoder:(NSCoder *)aDecoder |
|||
{ |
|||
if ((self = [super initWithCoder:aDecoder])) |
|||
{ |
|||
[self setUp]; |
|||
} |
|||
return self; |
|||
} |
|||
|
|||
- (void)dealloc |
|||
{ |
|||
if (_dotShape > LAST_SHAPE) CGPathRelease(_dotShape); |
|||
if (_selectedDotShape > LAST_SHAPE) CGPathRelease(_selectedDotShape); |
|||
} |
|||
|
|||
- (CGSize)sizeForNumberOfPages:(__unused NSInteger)pageCount |
|||
{ |
|||
CGFloat width = _dotSize + (_dotSize + _dotSpacing) * (_numberOfPages - 1); |
|||
return _vertical? CGSizeMake(_dotSize, width): CGSizeMake(width, _dotSize); |
|||
} |
|||
|
|||
- (void)updateCurrentPageDisplay |
|||
{ |
|||
[self setNeedsDisplay]; |
|||
} |
|||
|
|||
- (void)drawRect:(__unused CGRect)rect |
|||
{ |
|||
if (_numberOfPages > 1 || !_hidesForSinglePage) |
|||
{ |
|||
CGContextRef context = UIGraphicsGetCurrentContext(); |
|||
CGSize size = [self sizeForNumberOfPages:_numberOfPages]; |
|||
if (_vertical) |
|||
{ |
|||
CGContextTranslateCTM(context, self.frame.size.width / 2, (self.frame.size.height - size.height) / 2); |
|||
} |
|||
else |
|||
{ |
|||
CGContextTranslateCTM(context, (self.frame.size.width - size.width) / 2, self.frame.size.height / 2); |
|||
} |
|||
|
|||
for (int i = 0; i < _numberOfPages; i++) |
|||
{ |
|||
UIImage *dotImage = nil; |
|||
UIColor *dotColor = nil; |
|||
CGPathRef dotShape = NULL; |
|||
CGFloat dotSize = 0; |
|||
UIColor *dotShadowColor = nil; |
|||
CGSize dotShadowOffset = CGSizeZero; |
|||
CGFloat dotShadowBlur = 0; |
|||
|
|||
if (i == _currentPage) |
|||
{ |
|||
[_selectedDotColor setFill]; |
|||
dotImage = [_delegate pageControl:self selectedImageForDotAtIndex:i] ?: _selectedDotImage; |
|||
dotShape = [_delegate pageControl:self selectedShapeForDotAtIndex:i] ?: _selectedDotShape ?: _dotShape; |
|||
dotColor = [_delegate pageControl:self selectedColorForDotAtIndex:i] ?: _selectedDotColor ?: [UIColor blackColor]; |
|||
dotShadowBlur = _selectedDotShadowBlur; |
|||
dotShadowColor = _selectedDotShadowColor; |
|||
dotShadowOffset = _selectedDotShadowOffset; |
|||
dotSize = _selectedDotSize ?: _dotSize; |
|||
} |
|||
else |
|||
{ |
|||
[_dotColor setFill]; |
|||
dotImage = [_delegate pageControl:self imageForDotAtIndex:i] ?: _dotImage; |
|||
dotShape = [_delegate pageControl:self shapeForDotAtIndex:i] ?: _dotShape; |
|||
dotColor = [_delegate pageControl:self colorForDotAtIndex:i] ?: _dotColor; |
|||
if (!dotColor) |
|||
{ |
|||
//fall back to selected dot color with reduced alpha |
|||
dotColor = [_delegate pageControl:self selectedColorForDotAtIndex:i] ?: _selectedDotColor ?: [UIColor blackColor]; |
|||
dotColor = [dotColor colorWithAlphaComponent:0.25f]; |
|||
} |
|||
dotShadowBlur = _dotShadowBlur; |
|||
dotShadowColor = _dotShadowColor; |
|||
dotShadowOffset = _dotShadowOffset; |
|||
dotSize = _dotSize; |
|||
} |
|||
|
|||
CGContextSaveGState(context); |
|||
CGFloat offset = (_dotSize + _dotSpacing) * i + _dotSize / 2; |
|||
CGContextTranslateCTM(context, _vertical? 0: offset, _vertical? offset: 0); |
|||
|
|||
if (dotShadowColor && ![dotShadowColor isEqual:[UIColor clearColor]]) |
|||
{ |
|||
CGContextSetShadowWithColor(context, dotShadowOffset, dotShadowBlur, dotShadowColor.CGColor); |
|||
} |
|||
if (dotImage) |
|||
{ |
|||
[dotImage drawInRect:CGRectMake(-dotImage.size.width / 2, -dotImage.size.height / 2, dotImage.size.width, dotImage.size.height)]; |
|||
} |
|||
else |
|||
{ |
|||
[dotColor setFill]; |
|||
if (!dotShape || dotShape == FXPageControlDotShapeCircle) |
|||
{ |
|||
CGContextFillEllipseInRect(context, CGRectMake(-dotSize / 2, -dotSize / 2, dotSize, dotSize)); |
|||
} |
|||
else if (dotShape == FXPageControlDotShapeSquare) |
|||
{ |
|||
CGContextFillRect(context, CGRectMake(-dotSize / 2, -dotSize / 2, dotSize, dotSize)); |
|||
} |
|||
else if (dotShape == FXPageControlDotShapeTriangle) |
|||
{ |
|||
CGContextBeginPath(context); |
|||
CGContextMoveToPoint(context, 0, -dotSize / 2); |
|||
CGContextAddLineToPoint(context, dotSize / 2, dotSize / 2); |
|||
CGContextAddLineToPoint(context, -dotSize / 2, dotSize / 2); |
|||
CGContextAddLineToPoint(context, 0, -dotSize / 2); |
|||
CGContextFillPath(context); |
|||
} |
|||
else |
|||
{ |
|||
CGContextBeginPath(context); |
|||
CGContextAddPath(context, dotShape); |
|||
CGContextFillPath(context); |
|||
} |
|||
} |
|||
CGContextRestoreGState(context); |
|||
} |
|||
} |
|||
} |
|||
|
|||
- (NSInteger)clampedPageValue:(NSInteger)page |
|||
{ |
|||
if (_wrapEnabled) |
|||
{ |
|||
return _numberOfPages? (page + _numberOfPages) % _numberOfPages: 0; |
|||
} |
|||
else |
|||
{ |
|||
return MIN(MAX(0, page), _numberOfPages - 1); |
|||
} |
|||
} |
|||
|
|||
- (void)setDotImage:(UIImage *)dotImage |
|||
{ |
|||
if (_dotImage != dotImage) |
|||
{ |
|||
_dotImage = dotImage; |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)setDotShape:(CGPathRef)dotShape |
|||
{ |
|||
if (_dotShape != dotShape) |
|||
{ |
|||
if (_dotShape > LAST_SHAPE) CGPathRelease(_dotShape); |
|||
_dotShape = dotShape; |
|||
if (_dotShape > LAST_SHAPE) CGPathRetain(_dotShape); |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)setDotSize:(CGFloat)dotSize |
|||
{ |
|||
if (ABS(_dotSize - dotSize) > 0.001) |
|||
{ |
|||
_dotSize = dotSize; |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)setDotColor:(UIColor *)dotColor |
|||
{ |
|||
if (_dotColor != dotColor) |
|||
{ |
|||
_dotColor = dotColor; |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)setDotShadowColor:(UIColor *)dotColor |
|||
{ |
|||
if (_dotShadowColor != dotColor) |
|||
{ |
|||
_dotShadowColor = dotColor; |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)setDotShadowBlur:(CGFloat)dotShadowBlur |
|||
{ |
|||
if (ABS(_dotShadowBlur - dotShadowBlur) > 0.001) |
|||
{ |
|||
_dotShadowBlur = dotShadowBlur; |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)setDotShadowOffset:(CGSize)dotShadowOffset |
|||
{ |
|||
if (!CGSizeEqualToSize(_dotShadowOffset, dotShadowOffset)) |
|||
{ |
|||
_dotShadowOffset = dotShadowOffset; |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)setSelectedDotImage:(UIImage *)dotImage |
|||
{ |
|||
if (_selectedDotImage != dotImage) |
|||
{ |
|||
_selectedDotImage = dotImage; |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)setSelectedDotColor:(UIColor *)dotColor |
|||
{ |
|||
if (_selectedDotColor != dotColor) |
|||
{ |
|||
_selectedDotColor = dotColor; |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)setSelectedDotShape:(CGPathRef)dotShape |
|||
{ |
|||
if (_selectedDotShape != dotShape) |
|||
{ |
|||
if (_selectedDotShape > LAST_SHAPE) CGPathRelease(_selectedDotShape); |
|||
_selectedDotShape = dotShape; |
|||
if (_selectedDotShape > LAST_SHAPE) CGPathRetain(_selectedDotShape); |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)setSelectedDotSize:(CGFloat)dotSize |
|||
{ |
|||
if (ABS(_selectedDotSize - dotSize) > 0.001) |
|||
{ |
|||
_selectedDotSize = dotSize; |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)setSelectedDotShadowColor:(UIColor *)dotColor |
|||
{ |
|||
if (_selectedDotShadowColor != dotColor) |
|||
{ |
|||
_selectedDotShadowColor = dotColor; |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)setSelectedDotShadowBlur:(CGFloat)dotShadowBlur |
|||
{ |
|||
if (ABS(_selectedDotShadowBlur - dotShadowBlur) > 0.001) |
|||
{ |
|||
_selectedDotShadowBlur = dotShadowBlur; |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)setSelectedDotShadowOffset:(CGSize)dotShadowOffset |
|||
{ |
|||
if (!CGSizeEqualToSize(_selectedDotShadowOffset, dotShadowOffset)) |
|||
{ |
|||
_selectedDotShadowOffset = dotShadowOffset; |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)setDotSpacing:(CGFloat)dotSpacing |
|||
{ |
|||
if (ABS(_dotSpacing - dotSpacing) > 0.001) |
|||
{ |
|||
_dotSpacing = dotSpacing; |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)setDelegate:(id<FXPageControlDelegate>)delegate |
|||
{ |
|||
if (_delegate != delegate) |
|||
{ |
|||
_delegate = delegate; |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)setCurrentPage:(NSInteger)page |
|||
{ |
|||
_currentPage = [self clampedPageValue:page]; |
|||
[self setNeedsDisplay]; |
|||
} |
|||
|
|||
- (void)setNumberOfPages:(NSInteger)pages |
|||
{ |
|||
if (_numberOfPages != pages) |
|||
{ |
|||
_numberOfPages = pages; |
|||
if (_currentPage >= pages) |
|||
{ |
|||
_currentPage = pages - 1; |
|||
} |
|||
[self setNeedsDisplay]; |
|||
} |
|||
} |
|||
|
|||
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event |
|||
{ |
|||
CGPoint point = [touch locationInView:self]; |
|||
BOOL forward = _vertical? (point.y > self.frame.size.height / 2): (point.x > self.frame.size.width / 2); |
|||
_currentPage = [self clampedPageValue:_currentPage + (forward? 1: -1)]; |
|||
if (!_defersCurrentPageDisplay) |
|||
{ |
|||
[self setNeedsDisplay]; |
|||
} |
|||
[self sendActionsForControlEvents:UIControlEventValueChanged]; |
|||
[super endTrackingWithTouch:touch withEvent:event]; |
|||
} |
|||
|
|||
- (CGSize)sizeThatFits:(__unused CGSize)size |
|||
{ |
|||
CGSize dotSize = [self sizeForNumberOfPages:_numberOfPages]; |
|||
if (_selectedDotSize) |
|||
{ |
|||
CGFloat width = (_selectedDotSize - _dotSize); |
|||
CGFloat height = MAX(36, MAX(_dotSize, _selectedDotSize)); |
|||
dotSize.width = _vertical? height: dotSize.width + width; |
|||
dotSize.height = _vertical? dotSize.height + width: height; |
|||
|
|||
} |
|||
if ((_dotShadowColor && ![_dotShadowColor isEqual:[UIColor clearColor]]) || |
|||
(_selectedDotShadowColor && ![_selectedDotShadowColor isEqual:[UIColor clearColor]])) |
|||
{ |
|||
dotSize.width += MAX(_dotShadowOffset.width, _selectedDotShadowOffset.width) * 2; |
|||
dotSize.height += MAX(_dotShadowOffset.height, _selectedDotShadowOffset.height) * 2; |
|||
dotSize.width += MAX(_dotShadowBlur, _selectedDotShadowBlur) * 2; |
|||
dotSize.height += MAX(_dotShadowBlur, _selectedDotShadowBlur) * 2; |
|||
} |
|||
return dotSize; |
|||
} |
|||
|
|||
- (CGSize)intrinsicContentSize |
|||
{ |
|||
return [self sizeThatFits:self.bounds.size]; |
|||
} |
|||
|
|||
@end |
@ -0,0 +1,80 @@ |
|||
// IndicatorInfo.swift |
|||
// XLPagerTabStrip ( https://github.com/xmartlabs/XLPagerTabStrip ) |
|||
// |
|||
// Copyright (c) 2017 Xmartlabs ( http://xmartlabs.com ) |
|||
// |
|||
// |
|||
// Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
// of this software and associated documentation files (the "Software"), to deal |
|||
// in the Software without restriction, including without limitation the rights |
|||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
// copies of the Software, and to permit persons to whom the Software is |
|||
// furnished to do so, subject to the following conditions: |
|||
// |
|||
// The above copyright notice and this permission notice shall be included in |
|||
// all copies or substantial portions of the Software. |
|||
// |
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
// THE SOFTWARE. |
|||
|
|||
import Foundation |
|||
|
|||
public struct IndicatorInfo { |
|||
|
|||
public var title: String? |
|||
public var image: UIImage? |
|||
public var highlightedImage: UIImage? |
|||
public var accessibilityLabel: String? |
|||
public var userInfo: Any? |
|||
|
|||
public init(title: String?) { |
|||
self.title = title |
|||
self.accessibilityLabel = title |
|||
} |
|||
|
|||
public init(image: UIImage?, highlightedImage: UIImage? = nil, userInfo: Any? = nil) { |
|||
self.image = image |
|||
self.highlightedImage = highlightedImage |
|||
self.userInfo = userInfo |
|||
} |
|||
|
|||
public init(title: String?, image: UIImage?, highlightedImage: UIImage? = nil, userInfo: Any? = nil) { |
|||
self.title = title |
|||
self.accessibilityLabel = title |
|||
self.image = image |
|||
self.highlightedImage = highlightedImage |
|||
self.userInfo = userInfo |
|||
} |
|||
|
|||
public init(title: String?, accessibilityLabel:String?, image: UIImage?, highlightedImage: UIImage? = nil, userInfo: Any? = nil) { |
|||
self.title = title |
|||
self.accessibilityLabel = accessibilityLabel |
|||
self.image = image |
|||
self.highlightedImage = highlightedImage |
|||
self.userInfo = userInfo |
|||
} |
|||
|
|||
} |
|||
|
|||
extension IndicatorInfo : ExpressibleByStringLiteral { |
|||
|
|||
public init(stringLiteral value: String) { |
|||
title = value |
|||
accessibilityLabel = value |
|||
} |
|||
|
|||
public init(extendedGraphemeClusterLiteral value: String) { |
|||
title = value |
|||
accessibilityLabel = value |
|||
} |
|||
|
|||
public init(unicodeScalarLiteral value: String) { |
|||
title = value |
|||
accessibilityLabel = value |
|||
} |
|||
} |
@ -0,0 +1,58 @@ |
|||
// PagerTabStripOptions.swift |
|||
// XLPagerTabStrip ( https://github.com/xmartlabs/XLPagerTabStrip ) |
|||
// |
|||
// Copyright (c) 2017 Xmartlabs ( http://xmartlabs.com ) |
|||
// |
|||
// |
|||
// Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
// of this software and associated documentation files (the "Software"), to deal |
|||
// in the Software without restriction, including without limitation the rights |
|||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
// copies of the Software, and to permit persons to whom the Software is |
|||
// furnished to do so, subject to the following conditions: |
|||
// |
|||
// The above copyright notice and this permission notice shall be included in |
|||
// all copies or substantial portions of the Software. |
|||
// |
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
// THE SOFTWARE. |
|||
|
|||
import Foundation |
|||
|
|||
public enum PagerTabStripBehaviour { |
|||
|
|||
case common(skipIntermediateViewControllers: Bool) |
|||
case progressive(skipIntermediateViewControllers: Bool, elasticIndicatorLimit: Bool) |
|||
|
|||
public var skipIntermediateViewControllers: Bool { |
|||
switch self { |
|||
case .common(let skipIntermediateViewControllers): |
|||
return skipIntermediateViewControllers |
|||
case .progressive(let skipIntermediateViewControllers, _): |
|||
return skipIntermediateViewControllers |
|||
} |
|||
} |
|||
|
|||
public var isProgressiveIndicator: Bool { |
|||
switch self { |
|||
case .common: |
|||
return false |
|||
case .progressive: |
|||
return true |
|||
} |
|||
} |
|||
|
|||
public var isElasticIndicatorLimit: Bool { |
|||
switch self { |
|||
case .common: |
|||
return false |
|||
case .progressive(_, let elasticIndicatorLimit): |
|||
return elasticIndicatorLimit |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,31 @@ |
|||
// PagerTabStripError.swift |
|||
// XLPagerTabStrip ( https://github.com/xmartlabs/XLPagerTabStrip ) |
|||
// |
|||
// Copyright (c) 2017 Xmartlabs ( http://xmartlabs.com ) |
|||
// |
|||
// |
|||
// Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
// of this software and associated documentation files (the "Software"), to deal |
|||
// in the Software without restriction, including without limitation the rights |
|||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
// copies of the Software, and to permit persons to whom the Software is |
|||
// furnished to do so, subject to the following conditions: |
|||
// |
|||
// The above copyright notice and this permission notice shall be included in |
|||
// all copies or substantial portions of the Software. |
|||
// |
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
// THE SOFTWARE. |
|||
|
|||
import Foundation |
|||
|
|||
public enum PagerTabStripError: Error { |
|||
|
|||
case viewControllerOutOfBounds |
|||
|
|||
} |
@ -0,0 +1,396 @@ |
|||
// PagerTabStripViewController.swift |
|||
// XLPagerTabStrip ( https://github.com/xmartlabs/XLPagerTabStrip ) |
|||
// |
|||
// Copyright (c) 2017 Xmartlabs ( http://xmartlabs.com ) |
|||
// |
|||
// |
|||
// Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
// of this software and associated documentation files (the "Software"), to deal |
|||
// in the Software without restriction, including without limitation the rights |
|||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
// copies of the Software, and to permit persons to whom the Software is |
|||
// furnished to do so, subject to the following conditions: |
|||
// |
|||
// The above copyright notice and this permission notice shall be included in |
|||
// all copies or substantial portions of the Software. |
|||
// |
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
// THE SOFTWARE. |
|||
|
|||
import Foundation |
|||
|
|||
// MARK: Protocols |
|||
|
|||
public protocol IndicatorInfoProvider { |
|||
|
|||
func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo |
|||
|
|||
} |
|||
|
|||
public protocol PagerTabStripDelegate: class { |
|||
|
|||
func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int) |
|||
} |
|||
|
|||
public protocol PagerTabStripIsProgressiveDelegate: PagerTabStripDelegate { |
|||
|
|||
func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool) |
|||
} |
|||
|
|||
public protocol PagerTabStripDataSource: class { |
|||
|
|||
func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] |
|||
} |
|||
|
|||
// MARK: PagerTabStripViewController |
|||
|
|||
open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate { |
|||
|
|||
@IBOutlet weak public var containerView: UIScrollView! |
|||
|
|||
open weak var delegate: PagerTabStripDelegate? |
|||
open weak var datasource: PagerTabStripDataSource? |
|||
|
|||
open var pagerBehaviour = PagerTabStripBehaviour.progressive(skipIntermediateViewControllers: true, elasticIndicatorLimit: true) |
|||
|
|||
open private(set) var viewControllers = [UIViewController]() |
|||
open private(set) var currentIndex = 0 |
|||
open private(set) var preCurrentIndex = 0 // used *only* to store the index to which move when the pager becomes visible |
|||
|
|||
open var pageWidth: CGFloat { |
|||
return containerView.bounds.width |
|||
} |
|||
|
|||
open var scrollPercentage: CGFloat { |
|||
if swipeDirection != .right { |
|||
let module = fmod(containerView.contentOffset.x, pageWidth) |
|||
return module == 0.0 ? 1.0 : module / pageWidth |
|||
} |
|||
return 1 - fmod(containerView.contentOffset.x >= 0 ? containerView.contentOffset.x : pageWidth + containerView.contentOffset.x, pageWidth) / pageWidth |
|||
} |
|||
|
|||
open var swipeDirection: SwipeDirection { |
|||
if containerView.contentOffset.x > lastContentOffset { |
|||
return .left |
|||
} else if containerView.contentOffset.x < lastContentOffset { |
|||
return .right |
|||
} |
|||
return .none |
|||
} |
|||
|
|||
override open func viewDidLoad() { |
|||
super.viewDidLoad() |
|||
let conteinerViewAux = containerView ?? { |
|||
let containerView = UIScrollView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)) |
|||
containerView.autoresizingMask = [.flexibleWidth, .flexibleHeight] |
|||
return containerView |
|||
}() |
|||
containerView = conteinerViewAux |
|||
if containerView.superview == nil { |
|||
view.addSubview(containerView) |
|||
} |
|||
containerView.bounces = true |
|||
containerView.alwaysBounceHorizontal = true |
|||
containerView.alwaysBounceVertical = false |
|||
containerView.scrollsToTop = false |
|||
containerView.delegate = self |
|||
containerView.showsVerticalScrollIndicator = false |
|||
containerView.showsHorizontalScrollIndicator = false |
|||
containerView.isPagingEnabled = true |
|||
reloadViewControllers() |
|||
|
|||
let childController = viewControllers[currentIndex] |
|||
addChildViewController(childController) |
|||
childController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth] |
|||
containerView.addSubview(childController.view) |
|||
childController.didMove(toParentViewController: self) |
|||
} |
|||
|
|||
open override func viewWillAppear(_ animated: Bool) { |
|||
super.viewWillAppear(animated) |
|||
isViewAppearing = true |
|||
childViewControllers.forEach { $0.beginAppearanceTransition(true, animated: animated) } |
|||
} |
|||
|
|||
override open func viewDidAppear(_ animated: Bool) { |
|||
super.viewDidAppear(animated) |
|||
lastSize = containerView.bounds.size |
|||
updateIfNeeded() |
|||
let needToUpdateCurrentChild = preCurrentIndex != currentIndex |
|||
if needToUpdateCurrentChild { |
|||
moveToViewController(at: preCurrentIndex) |
|||
} |
|||
isViewAppearing = false |
|||
childViewControllers.forEach { $0.endAppearanceTransition() } |
|||
} |
|||
|
|||
open override func viewWillDisappear(_ animated: Bool) { |
|||
super.viewWillDisappear(animated) |
|||
childViewControllers.forEach { $0.beginAppearanceTransition(false, animated: animated) } |
|||
} |
|||
|
|||
open override func viewDidDisappear(_ animated: Bool) { |
|||
super.viewDidDisappear(animated) |
|||
childViewControllers.forEach { $0.endAppearanceTransition() } |
|||
} |
|||
|
|||
override open func viewDidLayoutSubviews() { |
|||
super.viewDidLayoutSubviews() |
|||
updateIfNeeded() |
|||
} |
|||
|
|||
open override var shouldAutomaticallyForwardAppearanceMethods: Bool { |
|||
return false |
|||
} |
|||
|
|||
open func moveToViewController(at index: Int, animated: Bool = true) { |
|||
guard isViewLoaded && view.window != nil && currentIndex != index else { |
|||
preCurrentIndex = index |
|||
return |
|||
} |
|||
|
|||
if animated && pagerBehaviour.skipIntermediateViewControllers && abs(currentIndex - index) > 1 { |
|||
var tmpViewControllers = viewControllers |
|||
let currentChildVC = viewControllers[currentIndex] |
|||
let fromIndex = currentIndex < index ? index - 1 : index + 1 |
|||
let fromChildVC = viewControllers[fromIndex] |
|||
tmpViewControllers[currentIndex] = fromChildVC |
|||
tmpViewControllers[fromIndex] = currentChildVC |
|||
pagerTabStripChildViewControllersForScrolling = tmpViewControllers |
|||
containerView.setContentOffset(CGPoint(x: pageOffsetForChild(at: fromIndex), y: 0), animated: false) |
|||
(navigationController?.view ?? view).isUserInteractionEnabled = !animated |
|||
containerView.setContentOffset(CGPoint(x: pageOffsetForChild(at: index), y: 0), animated: true) |
|||
} else { |
|||
(navigationController?.view ?? view).isUserInteractionEnabled = !animated |
|||
containerView.setContentOffset(CGPoint(x: pageOffsetForChild(at: index), y: 0), animated: animated) |
|||
} |
|||
} |
|||
|
|||
open func moveTo(viewController: UIViewController, animated: Bool = true) { |
|||
moveToViewController(at: viewControllers.index(of: viewController)!, animated: animated) |
|||
} |
|||
|
|||
// MARK: - PagerTabStripDataSource |
|||
|
|||
open func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { |
|||
assertionFailure("Sub-class must implement the PagerTabStripDataSource viewControllers(for:) method") |
|||
return [] |
|||
} |
|||
|
|||
// MARK: - Helpers |
|||
|
|||
open func updateIfNeeded() { |
|||
if isViewLoaded && !lastSize.equalTo(containerView.bounds.size) { |
|||
updateContent() |
|||
} |
|||
} |
|||
|
|||
open func canMoveTo(index: Int) -> Bool { |
|||
return currentIndex != index && viewControllers.count > index |
|||
} |
|||
|
|||
open func pageOffsetForChild(at index: Int) -> CGFloat { |
|||
return CGFloat(index) * containerView.bounds.width |
|||
} |
|||
|
|||
open func offsetForChild(at index: Int) -> CGFloat { |
|||
return (CGFloat(index) * containerView.bounds.width) + ((containerView.bounds.width - view.bounds.width) * 0.5) |
|||
} |
|||
|
|||
open func offsetForChild(viewController: UIViewController) throws -> CGFloat { |
|||
guard let index = viewControllers.index(of: viewController) else { |
|||
throw PagerTabStripError.viewControllerOutOfBounds |
|||
} |
|||
return offsetForChild(at: index) |
|||
} |
|||
|
|||
open func pageFor(contentOffset: CGFloat) -> Int { |
|||
let result = virtualPageFor(contentOffset: contentOffset) |
|||
return pageFor(virtualPage: result) |
|||
} |
|||
|
|||
open func virtualPageFor(contentOffset: CGFloat) -> Int { |
|||
return Int((contentOffset + 1.5 * pageWidth) / pageWidth) - 1 |
|||
} |
|||
|
|||
open func pageFor(virtualPage: Int) -> Int { |
|||
if virtualPage < 0 { |
|||
return 0 |
|||
} |
|||
if virtualPage > viewControllers.count - 1 { |
|||
return viewControllers.count - 1 |
|||
} |
|||
return virtualPage |
|||
} |
|||
|
|||
open func updateContent() { |
|||
if lastSize.width != containerView.bounds.size.width { |
|||
lastSize = containerView.bounds.size |
|||
containerView.contentOffset = CGPoint(x: pageOffsetForChild(at: currentIndex), y: 0) |
|||
} |
|||
lastSize = containerView.bounds.size |
|||
|
|||
let pagerViewControllers = pagerTabStripChildViewControllersForScrolling ?? viewControllers |
|||
containerView.contentSize = CGSize(width: containerView.bounds.width * CGFloat(pagerViewControllers.count), height: containerView.contentSize.height) |
|||
|
|||
for (index, childController) in pagerViewControllers.enumerated() { |
|||
let pageOffsetForChild = self.pageOffsetForChild(at: index) |
|||
if fabs(containerView.contentOffset.x - pageOffsetForChild) < containerView.bounds.width { |
|||
if childController.parent != nil { |
|||
childController.view.frame = CGRect(x: offsetForChild(at: index), y: 0, width: view.bounds.width, height: containerView.bounds.height) |
|||
childController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth] |
|||
} else { |
|||
childController.beginAppearanceTransition(true, animated: false) |
|||
addChildViewController(childController) |
|||
childController.view.frame = CGRect(x: offsetForChild(at: index), y: 0, width: view.bounds.width, height: containerView.bounds.height) |
|||
childController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth] |
|||
containerView.addSubview(childController.view) |
|||
childController.didMove(toParentViewController: self) |
|||
childController.endAppearanceTransition() |
|||
} |
|||
} else { |
|||
if childController.parent != nil { |
|||
childController.beginAppearanceTransition(false, animated: false) |
|||
childController.willMove(toParentViewController: nil) |
|||
childController.view.removeFromSuperview() |
|||
childController.removeFromParentViewController() |
|||
childController.endAppearanceTransition() |
|||
} |
|||
} |
|||
} |
|||
|
|||
let oldCurrentIndex = currentIndex |
|||
let virtualPage = virtualPageFor(contentOffset: containerView.contentOffset.x) |
|||
let newCurrentIndex = pageFor(virtualPage: virtualPage) |
|||
currentIndex = newCurrentIndex |
|||
preCurrentIndex = currentIndex |
|||
let changeCurrentIndex = newCurrentIndex != oldCurrentIndex |
|||
|
|||
if let progressiveDelegate = self as? PagerTabStripIsProgressiveDelegate, pagerBehaviour.isProgressiveIndicator { |
|||
|
|||
let (fromIndex, toIndex, scrollPercentage) = progressiveIndicatorData(virtualPage) |
|||
progressiveDelegate.updateIndicator(for: self, fromIndex: fromIndex, toIndex: toIndex, withProgressPercentage: scrollPercentage, indexWasChanged: changeCurrentIndex) |
|||
} else { |
|||
delegate?.updateIndicator(for: self, fromIndex: min(oldCurrentIndex, pagerViewControllers.count - 1), toIndex: newCurrentIndex) |
|||
} |
|||
} |
|||
|
|||
open func reloadPagerTabStripView() { |
|||
guard isViewLoaded else { return } |
|||
for childController in viewControllers where childController.parent != nil { |
|||
childController.beginAppearanceTransition(false, animated: false) |
|||
childController.willMove(toParentViewController: nil) |
|||
childController.view.removeFromSuperview() |
|||
childController.removeFromParentViewController() |
|||
childController.endAppearanceTransition() |
|||
} |
|||
reloadViewControllers() |
|||
containerView.contentSize = CGSize(width: containerView.bounds.width * CGFloat(viewControllers.count), height: containerView.contentSize.height) |
|||
if currentIndex >= viewControllers.count { |
|||
currentIndex = viewControllers.count - 1 |
|||
} |
|||
preCurrentIndex = currentIndex |
|||
containerView.contentOffset = CGPoint(x: pageOffsetForChild(at: currentIndex), y: 0) |
|||
updateContent() |
|||
} |
|||
|
|||
// MARK: - UIScrollDelegate |
|||
|
|||
open func scrollViewDidScroll(_ scrollView: UIScrollView) { |
|||
if containerView == scrollView { |
|||
updateContent() |
|||
lastContentOffset = scrollView.contentOffset.x |
|||
} |
|||
} |
|||
|
|||
open func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { |
|||
if containerView == scrollView { |
|||
lastPageNumber = pageFor(contentOffset: scrollView.contentOffset.x) |
|||
} |
|||
} |
|||
|
|||
open func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) { |
|||
if containerView == scrollView { |
|||
pagerTabStripChildViewControllersForScrolling = nil |
|||
(navigationController?.view ?? view).isUserInteractionEnabled = true |
|||
updateContent() |
|||
} |
|||
} |
|||
|
|||
// MARK: - Orientation |
|||
|
|||
open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { |
|||
super.viewWillTransition(to: size, with: coordinator) |
|||
isViewRotating = true |
|||
pageBeforeRotate = currentIndex |
|||
coordinator.animate(alongsideTransition: nil) { [weak self] _ in |
|||
guard let me = self else { return } |
|||
me.isViewRotating = false |
|||
me.currentIndex = me.pageBeforeRotate |
|||
me.preCurrentIndex = me.currentIndex |
|||
me.updateIfNeeded() |
|||
} |
|||
} |
|||
|
|||
// MARK: Private |
|||
|
|||
private func progressiveIndicatorData(_ virtualPage: Int) -> (Int, Int, CGFloat) { |
|||
let count = viewControllers.count |
|||
var fromIndex = currentIndex |
|||
var toIndex = currentIndex |
|||
let direction = swipeDirection |
|||
|
|||
if direction == .left { |
|||
if virtualPage > count - 1 { |
|||
fromIndex = count - 1 |
|||
toIndex = count |
|||
} else { |
|||
if self.scrollPercentage >= 0.5 { |
|||
fromIndex = max(toIndex - 1, 0) |
|||
} else { |
|||
toIndex = fromIndex + 1 |
|||
} |
|||
} |
|||
} else if direction == .right { |
|||
if virtualPage < 0 { |
|||
fromIndex = 0 |
|||
toIndex = -1 |
|||
} else { |
|||
if self.scrollPercentage > 0.5 { |
|||
fromIndex = min(toIndex + 1, count - 1) |
|||
} else { |
|||
toIndex = fromIndex - 1 |
|||
} |
|||
} |
|||
} |
|||
let scrollPercentage = pagerBehaviour.isElasticIndicatorLimit ? self.scrollPercentage : ((toIndex < 0 || toIndex >= count) ? 0.0 : self.scrollPercentage) |
|||
return (fromIndex, toIndex, scrollPercentage) |
|||
} |
|||
|
|||
private func reloadViewControllers() { |
|||
guard let dataSource = datasource else { |
|||
fatalError("dataSource must not be nil") |
|||
} |
|||
viewControllers = dataSource.viewControllers(for: self) |
|||
// viewControllers |
|||
guard !viewControllers.isEmpty else { |
|||
fatalError("viewControllers(for:) should provide at least one child view controller") |
|||
} |
|||
viewControllers.forEach { if !($0 is IndicatorInfoProvider) { fatalError("Every view controller provided by PagerTabStripDataSource's viewControllers(for:) method must conform to IndicatorInfoProvider") }} |
|||
|
|||
} |
|||
|
|||
private var pagerTabStripChildViewControllersForScrolling: [UIViewController]? |
|||
private var lastPageNumber = 0 |
|||
private var lastContentOffset: CGFloat = 0.0 |
|||
private var pageBeforeRotate = 0 |
|||
private var lastSize = CGSize(width: 0, height: 0) |
|||
internal var isViewRotating = false |
|||
internal var isViewAppearing = false |
|||
|
|||
} |
@ -0,0 +1,111 @@ |
|||
// SegmentedPagerTabStripViewController.swift |
|||
// XLPagerTabStrip ( https://github.com/xmartlabs/XLPagerTabStrip ) |
|||
// |
|||
// Copyright (c) 2017 Xmartlabs ( http://xmartlabs.com ) |
|||
// |
|||
// |
|||
// Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
// of this software and associated documentation files (the "Software"), to deal |
|||
// in the Software without restriction, including without limitation the rights |
|||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
// copies of the Software, and to permit persons to whom the Software is |
|||
// furnished to do so, subject to the following conditions: |
|||
// |
|||
// The above copyright notice and this permission notice shall be included in |
|||
// all copies or substantial portions of the Software. |
|||
// |
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
// THE SOFTWARE. |
|||
|
|||
import Foundation |
|||
|
|||
public struct SegmentedPagerTabStripSettings { |
|||
|
|||
public struct Style { |
|||
public var segmentedControlColor: UIColor? |
|||
} |
|||
|
|||
public var style = Style() |
|||
} |
|||
|
|||
open class SegmentedPagerTabStripViewController: PagerTabStripViewController, PagerTabStripDataSource, PagerTabStripDelegate { |
|||
|
|||
@IBOutlet weak public var segmentedControl: UISegmentedControl! |
|||
|
|||
open var settings = SegmentedPagerTabStripSettings() |
|||
|
|||
public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { |
|||
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) |
|||
pagerBehaviour = PagerTabStripBehaviour.common(skipIntermediateViewControllers: true) |
|||
delegate = self |
|||
datasource = self |
|||
} |
|||
|
|||
required public init?(coder aDecoder: NSCoder) { |
|||
super.init(coder: aDecoder) |
|||
pagerBehaviour = PagerTabStripBehaviour.common(skipIntermediateViewControllers: true) |
|||
delegate = self |
|||
datasource = self |
|||
} |
|||
|
|||
private(set) var shouldUpdateSegmentedControl = true |
|||
|
|||
open override func viewDidLoad() { |
|||
super.viewDidLoad() |
|||
let auxSegmentedControl = segmentedControl ?? UISegmentedControl() |
|||
segmentedControl = auxSegmentedControl |
|||
if segmentedControl.superview == nil { |
|||
navigationItem.titleView = segmentedControl |
|||
} |
|||
segmentedControl.tintColor = settings.style.segmentedControlColor ?? segmentedControl.tintColor |
|||
segmentedControl.addTarget(self, action: #selector(SegmentedPagerTabStripViewController.segmentedControlChanged(_:)), for: .valueChanged) |
|||
reloadSegmentedControl() |
|||
} |
|||
|
|||
open override func reloadPagerTabStripView() { |
|||
super.reloadPagerTabStripView() |
|||
if isViewLoaded { |
|||
reloadSegmentedControl() |
|||
} |
|||
} |
|||
|
|||
func reloadSegmentedControl() { |
|||
segmentedControl.removeAllSegments() |
|||
for (index, item) in viewControllers.enumerated() { |
|||
let child = item as! IndicatorInfoProvider // swiftlint:disable:this force_cast |
|||
if let image = child.indicatorInfo(for: self).image { |
|||
segmentedControl.insertSegment(with: image, at: index, animated: false) |
|||
} else { |
|||
segmentedControl.insertSegment(withTitle: child.indicatorInfo(for: self).title, at: index, animated: false) |
|||
} |
|||
} |
|||
segmentedControl.selectedSegmentIndex = currentIndex |
|||
} |
|||
|
|||
@objc func segmentedControlChanged(_ sender: UISegmentedControl) { |
|||
let index = sender.selectedSegmentIndex |
|||
updateIndicator(for: self, fromIndex: currentIndex, toIndex: index) |
|||
shouldUpdateSegmentedControl = false |
|||
moveToViewController(at: index) |
|||
} |
|||
|
|||
// MARK: - PagerTabStripDelegate |
|||
|
|||
open func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int) { |
|||
if shouldUpdateSegmentedControl { |
|||
segmentedControl.selectedSegmentIndex = toIndex |
|||
} |
|||
} |
|||
|
|||
// MARK: - UIScrollViewDelegate |
|||
|
|||
open override func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) { |
|||
super.scrollViewDidEndScrollingAnimation(scrollView) |
|||
shouldUpdateSegmentedControl = true |
|||
} |
|||
} |
@ -0,0 +1,31 @@ |
|||
// SwipeDirection.swift |
|||
// XLPagerTabStrip ( https://github.com/xmartlabs/XLPagerTabStrip ) |
|||
// |
|||
// Copyright (c) 2017 Xmartlabs ( http://xmartlabs.com ) |
|||
// |
|||
// |
|||
// Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
// of this software and associated documentation files (the "Software"), to deal |
|||
// in the Software without restriction, including without limitation the rights |
|||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
// copies of the Software, and to permit persons to whom the Software is |
|||
// furnished to do so, subject to the following conditions: |
|||
// |
|||
// The above copyright notice and this permission notice shall be included in |
|||
// all copies or substantial portions of the Software. |
|||
// |
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
// THE SOFTWARE. |
|||
|
|||
import Foundation |
|||
|
|||
public enum SwipeDirection { |
|||
case left |
|||
case right |
|||
case none |
|||
} |
@ -0,0 +1,234 @@ |
|||
// TwitterPagerTabStripViewController.swift |
|||
// XLPagerTabStrip ( https://github.com/xmartlabs/XLPagerTabStrip ) |
|||
// |
|||
// Copyright (c) 2017 Xmartlabs ( http://xmartlabs.com ) |
|||
// |
|||
// |
|||
// Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
// of this software and associated documentation files (the "Software"), to deal |
|||
// in the Software without restriction, including without limitation the rights |
|||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
// copies of the Software, and to permit persons to whom the Software is |
|||
// furnished to do so, subject to the following conditions: |
|||
// |
|||
// The above copyright notice and this permission notice shall be included in |
|||
// all copies or substantial portions of the Software. |
|||
// |
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
// THE SOFTWARE. |
|||
|
|||
import Foundation |
|||
|
|||
public struct TwitterPagerTabStripSettings { |
|||
|
|||
public struct Style { |
|||
public var dotColor = UIColor(white: 1, alpha: 0.4) |
|||
public var selectedDotColor = UIColor.white |
|||
public var portraitTitleFont = UIFont.systemFont(ofSize: 18) |
|||
public var landscapeTitleFont = UIFont.systemFont(ofSize: 15) |
|||
public var titleColor = UIColor.white |
|||
} |
|||
|
|||
public var style = Style() |
|||
} |
|||
|
|||
open class TwitterPagerTabStripViewController: PagerTabStripViewController, PagerTabStripDataSource, PagerTabStripIsProgressiveDelegate { |
|||
|
|||
open var settings = TwitterPagerTabStripSettings() |
|||
|
|||
public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { |
|||
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) |
|||
pagerBehaviour = .progressive(skipIntermediateViewControllers: true, elasticIndicatorLimit: true) |
|||
delegate = self |
|||
datasource = self |
|||
} |
|||
|
|||
required public init?(coder aDecoder: NSCoder) { |
|||
super.init(coder: aDecoder) |
|||
pagerBehaviour = .progressive(skipIntermediateViewControllers: true, elasticIndicatorLimit: true) |
|||
delegate = self |
|||
datasource = self |
|||
} |
|||
|
|||
open override func viewDidLoad() { |
|||
super.viewDidLoad() |
|||
|
|||
if titleView.superview == nil { |
|||
navigationItem.titleView = titleView |
|||
} |
|||
|
|||
// keep watching the frame of titleView |
|||
titleView.addObserver(self, forKeyPath: "frame", options: [.new, .old], context: nil) |
|||
|
|||
guard let navigationController = navigationController else { |
|||
fatalError("TwitterPagerTabStripViewController should be embedded in a UINavigationController") |
|||
} |
|||
titleView.frame = CGRect(x: 0, y: 0, width: navigationController.navigationBar.frame.width, height: navigationController.navigationBar.frame.height) |
|||
titleView.addSubview(titleScrollView) |
|||
titleView.addSubview(pageControl) |
|||
reloadNavigationViewItems() |
|||
} |
|||
|
|||
open override func reloadPagerTabStripView() { |
|||
super.reloadPagerTabStripView() |
|||
guard isViewLoaded else { return } |
|||
|
|||
reloadNavigationViewItems() |
|||
setNavigationViewItemsPosition(updateAlpha: true) |
|||
} |
|||
|
|||
// MARK: - PagerTabStripDelegate |
|||
|
|||
open func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool) { |
|||
|
|||
// move indicator scroll view |
|||
guard let distance = distanceValue else { return } |
|||
var xOffset: CGFloat = 0 |
|||
if fromIndex < toIndex { |
|||
xOffset = distance * CGFloat(fromIndex) + distance * progressPercentage |
|||
} else if fromIndex > toIndex { |
|||
xOffset = distance * CGFloat(fromIndex) - distance * progressPercentage |
|||
} else { |
|||
xOffset = distance * CGFloat(fromIndex) |
|||
} |
|||
|
|||
titleScrollView.contentOffset = CGPoint(x: xOffset, y: 0) |
|||
|
|||
// update alpha of titles |
|||
setAlphaWith(offset: xOffset, andDistance: distance) |
|||
|
|||
// update page control page |
|||
pageControl.currentPage = currentIndex |
|||
} |
|||
|
|||
open func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int) { |
|||
fatalError() |
|||
} |
|||
|
|||
open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { |
|||
guard object as AnyObject === titleView && keyPath == "frame" && change?[NSKeyValueChangeKey.kindKey] as? UInt == NSKeyValueChange.setting.rawValue else { return } |
|||
|
|||
let oldRect = (change![NSKeyValueChangeKey.oldKey]! as AnyObject).cgRectValue |
|||
let newRect = (change![NSKeyValueChangeKey.oldKey]! as AnyObject).cgRectValue |
|||
if (oldRect?.equalTo(newRect!))! { |
|||
titleScrollView.frame = CGRect(x: 0, y: 0, width: titleView.frame.width, height: titleScrollView.frame.height) |
|||
setNavigationViewItemsPosition(updateAlpha: true) |
|||
} |
|||
} |
|||
|
|||
deinit { |
|||
if isViewLoaded { |
|||
titleView.removeObserver(self, forKeyPath: "frame") |
|||
} |
|||
} |
|||
|
|||
open override func viewDidLayoutSubviews() { |
|||
super.viewDidLayoutSubviews() |
|||
setNavigationViewItemsPosition(updateAlpha: false) |
|||
} |
|||
|
|||
// MARK: - Helpers |
|||
|
|||
private lazy var titleView: UIView = { |
|||
let navigationView = UIView() |
|||
navigationView.autoresizingMask = [.flexibleWidth, .flexibleHeight] |
|||
return navigationView |
|||
}() |
|||
|
|||
private lazy var titleScrollView: UIScrollView = { [unowned self] in |
|||
let titleScrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 44)) |
|||
titleScrollView.autoresizingMask = [.flexibleWidth, .flexibleHeight] |
|||
titleScrollView.bounces = true |
|||
titleScrollView.scrollsToTop = false |
|||
titleScrollView.delegate = self |
|||
titleScrollView.showsVerticalScrollIndicator = false |
|||
titleScrollView.showsHorizontalScrollIndicator = false |
|||
titleScrollView.isPagingEnabled = true |
|||
titleScrollView.isUserInteractionEnabled = false |
|||
titleScrollView.alwaysBounceHorizontal = true |
|||
titleScrollView.alwaysBounceVertical = false |
|||
return titleScrollView |
|||
}() |
|||
|
|||
private lazy var pageControl: FXPageControl = { [unowned self] in |
|||
let pageControl = FXPageControl() |
|||
pageControl.backgroundColor = .clear |
|||
pageControl.dotSize = 3.8 |
|||
pageControl.dotSpacing = 4.0 |
|||
pageControl.dotColor = self.settings.style.dotColor |
|||
pageControl.selectedDotColor = self.settings.style.selectedDotColor |
|||
pageControl.isUserInteractionEnabled = false |
|||
return pageControl |
|||
}() |
|||
|
|||
private var childTitleLabels = [UILabel]() |
|||
|
|||
private func reloadNavigationViewItems() { |
|||
// remove all child view controller header labels |
|||
childTitleLabels.forEach { $0.removeFromSuperview() } |
|||
childTitleLabels.removeAll() |
|||
for (index, item) in viewControllers.enumerated() { |
|||
let child = item as! IndicatorInfoProvider // swiftlint:disable:this force_cast |
|||
let indicatorInfo = child.indicatorInfo(for: self) |
|||
let navTitleLabel: UILabel = { |
|||
let label = UILabel() |
|||
label.text = indicatorInfo.title |
|||
label.font = UIApplication.shared.statusBarOrientation.isPortrait ? settings.style.portraitTitleFont : settings.style.landscapeTitleFont |
|||
label.textColor = settings.style.titleColor |
|||
label.alpha = 0 |
|||
return label |
|||
}() |
|||
navTitleLabel.alpha = currentIndex == index ? 1 : 0 |
|||
navTitleLabel.textColor = settings.style.titleColor |
|||
titleScrollView.addSubview(navTitleLabel) |
|||
childTitleLabels.append(navTitleLabel) |
|||
} |
|||
} |
|||
|
|||
private func setNavigationViewItemsPosition(updateAlpha: Bool) { |
|||
guard let distance = distanceValue else { return } |
|||
let isPortrait = UIApplication.shared.statusBarOrientation.isPortrait |
|||
let navBarHeight: CGFloat = navigationController!.navigationBar.frame.size.height |
|||
for (index, label) in childTitleLabels.enumerated() { |
|||
if updateAlpha { |
|||
label.alpha = currentIndex == index ? 1 : 0 |
|||
} |
|||
label.font = isPortrait ? settings.style.portraitTitleFont : settings.style.landscapeTitleFont |
|||
let viewSize = label.intrinsicContentSize |
|||
let originX = distance - viewSize.width/2 + CGFloat(index) * distance |
|||
let originY = (CGFloat(navBarHeight) - viewSize.height) / 2 |
|||
label.frame = CGRect(x: originX, y: originY - 2, width: viewSize.width, height: viewSize.height) |
|||
label.tag = index |
|||
} |
|||
|
|||
let xOffset = distance * CGFloat(currentIndex) |
|||
titleScrollView.contentOffset = CGPoint(x: xOffset, y: 0) |
|||
|
|||
pageControl.numberOfPages = childTitleLabels.count |
|||
pageControl.currentPage = currentIndex |
|||
let viewSize = pageControl.sizeForNumber(ofPages: childTitleLabels.count) |
|||
let originX = distance - viewSize.width / 2 |
|||
pageControl.frame = CGRect(x: originX, y: navBarHeight - 10, width: viewSize.width, height: viewSize.height) |
|||
} |
|||
|
|||
private func setAlphaWith(offset: CGFloat, andDistance distance: CGFloat) { |
|||
for (index, label) in childTitleLabels.enumerated() { |
|||
label.alpha = { |
|||
if offset < distance * CGFloat(index) { |
|||
return (offset - distance * CGFloat(index - 1)) / distance |
|||
} else { |
|||
return 1 - ((offset - distance * CGFloat(index)) / distance) |
|||
} |
|||
}() |
|||
} |
|||
} |
|||
|
|||
private var distanceValue: CGFloat? { |
|||
return navigationController.map { $0.navigationBar.convert($0.navigationBar.center, to: titleView) }?.x |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue