diff --git a/APFancyPager/APFancyPagerViewController.swift b/APFancyPager/APFancyPagerViewController.swift index d27c0d9..03c3b39 100644 --- a/APFancyPager/APFancyPagerViewController.swift +++ b/APFancyPager/APFancyPagerViewController.swift @@ -10,7 +10,6 @@ import UIKit /// Our almighty class which is simply a UIViewController. Everything starts by using this view controller. open class APFancyPagerViewController: UIViewController, UIScrollViewDelegate { - /// Delegate variable, you don't have to set this. open weak var delegate: APFancyPagerDelegate? /// Datasource variable, you have to set this. @@ -29,7 +28,7 @@ open class APFancyPagerViewController: UIViewController, UIScrollViewDelegate { private var numberOfControllers: Int! /// This variable used for detection of the scroll direction. private var lastPercentage: CGFloat = 0.0 - + /// Some initialization codes. public required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) @@ -44,9 +43,9 @@ open class APFancyPagerViewController: UIViewController, UIScrollViewDelegate { open override func viewDidLoad() { super.viewDidLoad() - self.view.backgroundColor = UIColor.white + view.backgroundColor = UIColor.white - if (dataSource == nil) { + if dataSource == nil { return } @@ -57,9 +56,9 @@ open class APFancyPagerViewController: UIViewController, UIScrollViewDelegate { private func initializeViews() { reloadData() - headerContainerView.frame = CGRect(x: 0.0, y: 0.0, width: self.view.bounds.width, height: headerHeight) + headerContainerView.frame = CGRect(x: 0.0, y: 0.0, width: view.bounds.width, height: headerHeight) headerContainerView.backgroundColor = dataSource!.fancyPagerViewController(self, headerBackgroundColorForIndex: 0) - self.view.addSubview(headerContainerView) + view.addSubview(headerContainerView) headerTitleLabel.frame = CGRect(x: 0, y: Util.statusBarHeight(), width: headerContainerView.bounds.width, height: headerContainerView.frame.height - Util.statusBarHeight()) headerTitleLabel.text = dataSource!.fancyPagerViewController(self, headerStringForIndex: 0) @@ -70,15 +69,14 @@ open class APFancyPagerViewController: UIViewController, UIScrollViewDelegate { headerTitleLabel.adjustsFontSizeToFitWidth = true headerContainerView.addSubview(headerTitleLabel) - containerScrollView.frame = CGRect(x: 0, y: headerHeight, width: self.view.bounds.width, height: self.view.bounds.height - headerHeight) + containerScrollView.frame = CGRect(x: 0, y: headerHeight, width: view.bounds.width, height: view.bounds.height - headerHeight) containerScrollView.contentSize = CGSize(width: containerScrollView.bounds.width * CGFloat(numberOfControllers), height: containerScrollView.bounds.height) containerScrollView.delegate = self containerScrollView.bounces = false containerScrollView.isPagingEnabled = true - self.view.addSubview(containerScrollView) + view.addSubview(containerScrollView) addViewControllersToPager() - } /// This function adds view controllers to @@ -86,13 +84,12 @@ open class APFancyPagerViewController: UIViewController, UIScrollViewDelegate { /// parameters, does things by using the /// data source which is already provided. private func addViewControllersToPager() { - var xVal: CGFloat = 0.0 for index in 0...numberOfControllers { let currentVC = dataSource!.fancyPagerViewController(self, viewControllerForIndex: index) - self.addChild(currentVC) + addChild(currentVC) currentVC.view.frame = CGRect(x: xVal, y: 0.0, width: containerScrollView.bounds.width, height: containerScrollView.bounds.height) containerScrollView.addSubview(currentVC.view) currentVC.willMove(toParent: self) @@ -113,7 +110,7 @@ open class APFancyPagerViewController: UIViewController, UIScrollViewDelegate { - parameter animated: Should this scrolling be animated or not? */ open func scrollToPage(_ pageNumber: Int, animated: Bool) { - if (pageNumber >= 0 && pageNumber < numberOfControllers) { + if pageNumber >= 0 && pageNumber < numberOfControllers { containerScrollView.scrollRectToVisible(CGRect(x: containerScrollView.bounds.width * CGFloat(pageNumber), y: 0.0, width: containerScrollView.bounds.width, height: containerScrollView.bounds.height), animated: animated) } } @@ -127,7 +124,6 @@ open class APFancyPagerViewController: UIViewController, UIScrollViewDelegate { */ private func animateHeaderTitle(_ sourceString: String, _ destinationString: String, _ percentage: CGFloat) { - let maxLetterCount = max(sourceString.count, destinationString.count) let changingLetterCountTreshold = 1.0 / Double(maxLetterCount) let indexOfCharacter = Int(floor(Double(percentage) / changingLetterCountTreshold)) @@ -137,18 +133,18 @@ open class APFancyPagerViewController: UIViewController, UIScrollViewDelegate { var newString = "" - if (indexOfCharacter >= sourceString.count) { + if indexOfCharacter >= sourceString.count { for _ in 0...(maxLetterCount - sourceString.count) { tempSourceString.append(" ") } - } else if (indexOfCharacter >= destinationString.count) { + } else if indexOfCharacter >= destinationString.count { for _ in 0...(maxLetterCount - destinationString.count) { tempDestinationString.append(" ") } } newString = String(tempSourceString[indexOfCharacter...(tempSourceString.count - 1)]) - if (indexOfCharacter != 0) { + if indexOfCharacter != 0 { newString += String(tempDestinationString[0...indexOfCharacter - 1]) } @@ -162,16 +158,14 @@ open class APFancyPagerViewController: UIViewController, UIScrollViewDelegate { */ open func scrollViewDidScroll(_ scrollView: UIScrollView) { - let pageNumber = floorf(Float(scrollView.contentOffset.x / (scrollView.frame.size.width))) let percentageOffset = (scrollView.contentOffset.x / scrollView.frame.width) - CGFloat(pageNumber) - if (percentageOffset > 0.0) { - if (numberOfControllers > Int(pageNumber) + 1) { - - if (lastPercentage < percentageOffset) { + if percentageOffset > 0.0 { + if numberOfControllers > Int(pageNumber) + 1 { + if lastPercentage < percentageOffset { delegate?.fancyPagerViewController(self, isScrollingFromIndex: Int(pageNumber), toIndex: Int(pageNumber + 1), progress: percentageOffset) - } else if (lastPercentage > percentageOffset) { + } else if lastPercentage > percentageOffset { delegate?.fancyPagerViewController(self, isScrollingFromIndex: Int(pageNumber + 1), toIndex: Int(pageNumber), progress: 1.0 - percentageOffset) } lastPercentage = percentageOffset @@ -185,9 +179,9 @@ open class APFancyPagerViewController: UIViewController, UIScrollViewDelegate { let prevTitleString = dataSource!.fancyPagerViewController(self, headerStringForIndex: Int(pageNumber)) let nextTitleString = dataSource!.fancyPagerViewController(self, headerStringForIndex: Int(pageNumber + 1)) - headerContainerView.backgroundColor = UIColor.init(red: (prevBackgroundColor.rgba.red + ((nextBackgroundColor.rgba.red - prevBackgroundColor.rgba.red) * percentageOffset)), green: (prevBackgroundColor.rgba.green + ((nextBackgroundColor.rgba.green - prevBackgroundColor.rgba.green) * percentageOffset)), blue: (prevBackgroundColor.rgba.blue + ((nextBackgroundColor.rgba.blue - prevBackgroundColor.rgba.blue) * percentageOffset)), alpha: (prevBackgroundColor.rgba.alpha + ((nextBackgroundColor.rgba.alpha - prevBackgroundColor.rgba.alpha) * percentageOffset))) + headerContainerView.backgroundColor = UIColor(red: (prevBackgroundColor.rgba.red + ((nextBackgroundColor.rgba.red - prevBackgroundColor.rgba.red) * percentageOffset)), green: (prevBackgroundColor.rgba.green + ((nextBackgroundColor.rgba.green - prevBackgroundColor.rgba.green) * percentageOffset)), blue: (prevBackgroundColor.rgba.blue + ((nextBackgroundColor.rgba.blue - prevBackgroundColor.rgba.blue) * percentageOffset)), alpha: (prevBackgroundColor.rgba.alpha + ((nextBackgroundColor.rgba.alpha - prevBackgroundColor.rgba.alpha) * percentageOffset))) - headerTitleLabel.textColor = UIColor.init(red: (prevTitleColor.rgba.red + ((nextTitleColor.rgba.red - prevTitleColor.rgba.red) * percentageOffset)), green: (prevTitleColor.rgba.green + ((nextTitleColor.rgba.green - prevTitleColor.rgba.green) * percentageOffset)), blue: (prevTitleColor.rgba.blue + ((nextTitleColor.rgba.blue - prevTitleColor.rgba.blue) * percentageOffset)), alpha: (prevTitleColor.rgba.alpha + ((nextTitleColor.rgba.alpha - prevTitleColor.rgba.alpha) * percentageOffset))) + headerTitleLabel.textColor = UIColor(red: (prevTitleColor.rgba.red + ((nextTitleColor.rgba.red - prevTitleColor.rgba.red) * percentageOffset)), green: (prevTitleColor.rgba.green + ((nextTitleColor.rgba.green - prevTitleColor.rgba.green) * percentageOffset)), blue: (prevTitleColor.rgba.blue + ((nextTitleColor.rgba.blue - prevTitleColor.rgba.blue) * percentageOffset)), alpha: (prevTitleColor.rgba.alpha + ((nextTitleColor.rgba.alpha - prevTitleColor.rgba.alpha) * percentageOffset))) animateHeaderTitle(prevTitleString, nextTitleString, percentageOffset) } @@ -221,5 +215,4 @@ open class APFancyPagerViewController: UIViewController, UIScrollViewDelegate { delegate?.fancyPagerViewController(self, didScrollToIndex: Int(pageNumber)) } - } diff --git a/APFancyPager/Protocols/APFancyPagerDataSource.swift b/APFancyPager/Protocols/APFancyPagerDataSource.swift index 18165ad..76b2c47 100644 --- a/APFancyPager/Protocols/APFancyPagerDataSource.swift +++ b/APFancyPager/Protocols/APFancyPagerDataSource.swift @@ -10,7 +10,6 @@ import UIKit /// Data source protocol for APFancyPager @objc public protocol APFancyPagerDataSource: class { - /** You should implement this function in order to tell the number of your view controllers that will be used in the APFancyPager. - parameter fancyPagerViewController: APFancyPagerViewController object of currently using. @@ -62,5 +61,4 @@ import UIKit - returns: Header text color of the given index of the APFancyPager */ func fancyPagerViewController(_ fancyPagerViewController: APFancyPagerViewController, headerTextColorForIndex index: Int) -> UIColor - } diff --git a/APFancyPager/Protocols/APFancyPagerDelegate.swift b/APFancyPager/Protocols/APFancyPagerDelegate.swift index 6979a5a..be5d4a8 100644 --- a/APFancyPager/Protocols/APFancyPagerDelegate.swift +++ b/APFancyPager/Protocols/APFancyPagerDelegate.swift @@ -10,7 +10,6 @@ import UIKit /// Delegate protocol for APFancyPager @objc public protocol APFancyPagerDelegate: class { - /** You can implement this function in order to get information while the user is scrolling through the APFancyPager. - parameter fancyPagerViewController: APFancyPagerViewController object of currently using. @@ -29,7 +28,6 @@ import UIKit } public extension APFancyPagerDelegate { - public func fancyPagerViewController(_ fancyPagerViewController: APFancyPagerViewController, isScrollingFromIndex fromIndex: Int, toIndex: Int, progress: CGFloat) { return } diff --git a/APFancyPager/Util/Util.swift b/APFancyPager/Util/Util.swift index ee43631..b10fe17 100644 --- a/APFancyPager/Util/Util.swift +++ b/APFancyPager/Util/Util.swift @@ -10,16 +10,14 @@ import UIKit /// There are some utility things. open class Util { - /** An easy way to get status bar height. - + - returns: Status bar height. Simple, isn't it? */ class func statusBarHeight() -> CGFloat { return UIApplication.shared.statusBarFrame.height } - } extension UIColor { @@ -29,63 +27,74 @@ extension UIColor { var blue: CGFloat = 0 var alpha: CGFloat = 0 getRed(&red, green: &green, blue: &blue, alpha: &alpha) - + return (red, green, blue, alpha) } } extension String { - subscript (i: Int) -> Character { + subscript(i: Int) -> Character { return self[index(startIndex, offsetBy: i)] } - subscript (bounds: CountableRange) -> Substring { + + subscript(bounds: CountableRange) -> Substring { let start = index(startIndex, offsetBy: bounds.lowerBound) let end = index(startIndex, offsetBy: bounds.upperBound) return self[start ..< end] } - subscript (bounds: CountableClosedRange) -> Substring { + + subscript(bounds: CountableClosedRange) -> Substring { let start = index(startIndex, offsetBy: bounds.lowerBound) let end = index(startIndex, offsetBy: bounds.upperBound) return self[start ... end] } - subscript (bounds: CountablePartialRangeFrom) -> Substring { + + subscript(bounds: CountablePartialRangeFrom) -> Substring { let start = index(startIndex, offsetBy: bounds.lowerBound) let end = index(endIndex, offsetBy: -1) return self[start ... end] } - subscript (bounds: PartialRangeThrough) -> Substring { + + subscript(bounds: PartialRangeThrough) -> Substring { let end = index(startIndex, offsetBy: bounds.upperBound) return self[startIndex ... end] } - subscript (bounds: PartialRangeUpTo) -> Substring { + + subscript(bounds: PartialRangeUpTo) -> Substring { let end = index(startIndex, offsetBy: bounds.upperBound) return self[startIndex ..< end] } } + extension Substring { - subscript (i: Int) -> Character { + subscript(i: Int) -> Character { return self[index(startIndex, offsetBy: i)] } - subscript (bounds: CountableRange) -> Substring { + + subscript(bounds: CountableRange) -> Substring { let start = index(startIndex, offsetBy: bounds.lowerBound) let end = index(startIndex, offsetBy: bounds.upperBound) return self[start ..< end] } - subscript (bounds: CountableClosedRange) -> Substring { + + subscript(bounds: CountableClosedRange) -> Substring { let start = index(startIndex, offsetBy: bounds.lowerBound) let end = index(startIndex, offsetBy: bounds.upperBound) return self[start ... end] } - subscript (bounds: CountablePartialRangeFrom) -> Substring { + + subscript(bounds: CountablePartialRangeFrom) -> Substring { let start = index(startIndex, offsetBy: bounds.lowerBound) let end = index(endIndex, offsetBy: -1) return self[start ... end] } - subscript (bounds: PartialRangeThrough) -> Substring { + + subscript(bounds: PartialRangeThrough) -> Substring { let end = index(startIndex, offsetBy: bounds.upperBound) return self[startIndex ... end] } - subscript (bounds: PartialRangeUpTo) -> Substring { + + subscript(bounds: PartialRangeUpTo) -> Substring { let end = index(startIndex, offsetBy: bounds.upperBound) return self[startIndex ..< end] } diff --git a/APFancyPagerDemo/APFancyPagerDemo/AppDelegate.swift b/APFancyPagerDemo/APFancyPagerDemo/AppDelegate.swift index 53c4267..8322249 100644 --- a/APFancyPagerDemo/APFancyPagerDemo/AppDelegate.swift +++ b/APFancyPagerDemo/APFancyPagerDemo/AppDelegate.swift @@ -10,10 +10,8 @@ import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { - var window: UIWindow? - func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true @@ -40,7 +38,4 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } - - } - diff --git a/APFancyPagerDemo/APFancyPagerDemo/ViewController.swift b/APFancyPagerDemo/APFancyPagerDemo/ViewController.swift index 3abe91c..90aceb9 100644 --- a/APFancyPagerDemo/APFancyPagerDemo/ViewController.swift +++ b/APFancyPagerDemo/APFancyPagerDemo/ViewController.swift @@ -6,8 +6,8 @@ // Copyright © 2018 Apps. All rights reserved. // -import UIKit import APFancyPager +import UIKit class ViewController: APFancyPagerViewController, APFancyPagerDelegate, APFancyPagerDataSource { override func viewDidLoad() { @@ -32,20 +32,20 @@ class ViewController: APFancyPagerViewController, APFancyPagerDelegate, APFancyP let viewController = UIViewController() let button = UIButton(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0)) button.center = viewController.view.center - if (index == 0) { + if index == 0 { button.backgroundColor = UIColor.blue button.tag = 1 - } else if (index == 1) { + } else if index == 1 { button.backgroundColor = UIColor.red button.tag = 2 - } else if (index == 2) { + } else if index == 2 { button.backgroundColor = UIColor.green button.tag = 3 - } else if (index == 3) { + } else if index == 3 { button.backgroundColor = UIColor.orange button.tag = 0 } - button.addTarget(self, action: #selector(buttonClicked(_:)), for: .touchUpInside) + button.addTarget(self, action: #selector(self.buttonClicked(_:)), for: .touchUpInside) viewController.view.addSubview(button) return viewController } @@ -111,7 +111,4 @@ class ViewController: APFancyPagerViewController, APFancyPagerDelegate, APFancyP super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } - - } - diff --git a/README.md b/README.md index 097c7eb..44f0b45 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ + # APFancyPager A fancy pager view controller framework for Swift/Objective-C @@ -6,7 +7,6 @@ A fancy pager view controller framework for Swift/Objective-C [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![License](http://img.shields.io/cocoapods/l/APFancyPager.svg)](https://raw.githubusercontent.com/AppsComTr/APFancyPager/master/LICENSE) - ## Installation ### Cocoapods @@ -27,9 +27,8 @@ You can check the demo project to get the basics of the framework. Simply make your view controller extend APFancyPagerViewController and also implement the data source and optionally the delegate. - You should use the below data source function in order to specify the number of pages that APFancyPager will show. -``` +```swift func numberOfViewControllers(in fancyPagerViewController: APFancyPagerViewController) -> Int { return pages.count } @@ -37,41 +36,53 @@ func numberOfViewControllers(in fancyPagerViewController: APFancyPagerViewContro Also you should return your desired header height from the data source function below. -``` +```swift func heightForHeader(_ fancyPagerViewController: APFancyPagerViewController) -> CGFloat { return 100.0 } ``` -You should return related view controller into pages by using `fancyPagerViewController(_ fancyPagerViewController: APFancyPagerViewController, viewControllerForIndex index: Int) -> UIViewController` +You should return related view controller into pages by using +```swift +fancyPagerViewController(_ fancyPagerViewController: APFancyPagerViewController, viewControllerForIndex index: Int) -> UIViewController +``` Just return your view controller for related index. You want to show header titles for your fancy pager, don't you? Here, use below data source function o achieve this: -``` +```swift func fancyPagerViewController(_ fancyPagerViewController: APFancyPagerViewController, headerStringForIndex index: Int) -> String ``` For the text color of the header at index this: -``` +```swift func fancyPagerViewController(_ fancyPagerViewController: APFancyPagerViewController, headerTextColorForIndex index: Int) -> UIColor ``` and for the background color at index, this: -``` + +```swift func fancyPagerViewController(_ fancyPagerViewController: APFancyPagerViewController, headerBackgroundColorForIndex index: Int) -> UIColor ``` -There are 2 delegate functions which are `func fancyPagerViewController(_ fancyPagerViewController: APFancyPagerViewController, didScrollToIndex index: Int)` and `func fancyPagerViewController(_ fancyPagerViewController: APFancyPagerViewController, isScrollingFromIndex fromIndex: Int, toIndex: Int, progress: CGFloat)`. You can use them to get some useful (maybe not useful that much) informations about the APFancyPager. +There are 2 delegate functions which are +```swift +func fancyPagerViewController(_ fancyPagerViewController: APFancyPagerViewController, didScrollToIndex index: Int) +``` +and +```swift +func fancyPagerViewController(_ fancyPagerViewController: APFancyPagerViewController, isScrollingFromIndex fromIndex: Int, toIndex: Int, progress: CGFloat) +``` +You can use them to get some useful (maybe not useful that much) informations about the APFancyPager. The last but not least, you can use an extra function to scroll the APFancyPager to the any index directly. To achieve this, simply use below function: -``` + +```swift func scrollToPage(_ pageNumber: Int, animated: Bool) ``` ## Preview - ## License APFancyPager is released under the MIT license. See [LICENSE](https://github.com/AppsComTr/APFancyPager/blob/master/LICENSE) for details.