Skip to content

Commit

Permalink
code style fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsimsek committed Oct 31, 2018
1 parent 2b5ebef commit 7109e1b
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 70 deletions.
43 changes: 18 additions & 25 deletions APFancyPager/APFancyPagerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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
}

Expand All @@ -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)
Expand All @@ -70,29 +69,27 @@ 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
/// the **APFancyPager**. Does not take any
/// 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)
Expand All @@ -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)
}
}
Expand All @@ -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))
Expand All @@ -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])
}

Expand All @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -221,5 +215,4 @@ open class APFancyPagerViewController: UIViewController, UIScrollViewDelegate {

delegate?.fancyPagerViewController(self, didScrollToIndex: Int(pageNumber))
}

}
2 changes: 0 additions & 2 deletions APFancyPager/Protocols/APFancyPagerDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

}
2 changes: 0 additions & 2 deletions APFancyPager/Protocols/APFancyPagerDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -29,7 +28,6 @@ import UIKit
}

public extension APFancyPagerDelegate {

public func fancyPagerViewController(_ fancyPagerViewController: APFancyPagerViewController, isScrollingFromIndex fromIndex: Int, toIndex: Int, progress: CGFloat) {
return
}
Expand Down
41 changes: 25 additions & 16 deletions APFancyPager/Util/Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Int>) -> Substring {

subscript(bounds: CountableRange<Int>) -> Substring {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(startIndex, offsetBy: bounds.upperBound)
return self[start ..< end]
}
subscript (bounds: CountableClosedRange<Int>) -> Substring {

subscript(bounds: CountableClosedRange<Int>) -> Substring {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(startIndex, offsetBy: bounds.upperBound)
return self[start ... end]
}
subscript (bounds: CountablePartialRangeFrom<Int>) -> Substring {

subscript(bounds: CountablePartialRangeFrom<Int>) -> Substring {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(endIndex, offsetBy: -1)
return self[start ... end]
}
subscript (bounds: PartialRangeThrough<Int>) -> Substring {

subscript(bounds: PartialRangeThrough<Int>) -> Substring {
let end = index(startIndex, offsetBy: bounds.upperBound)
return self[startIndex ... end]
}
subscript (bounds: PartialRangeUpTo<Int>) -> Substring {

subscript(bounds: PartialRangeUpTo<Int>) -> 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<Int>) -> Substring {

subscript(bounds: CountableRange<Int>) -> Substring {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(startIndex, offsetBy: bounds.upperBound)
return self[start ..< end]
}
subscript (bounds: CountableClosedRange<Int>) -> Substring {

subscript(bounds: CountableClosedRange<Int>) -> Substring {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(startIndex, offsetBy: bounds.upperBound)
return self[start ... end]
}
subscript (bounds: CountablePartialRangeFrom<Int>) -> Substring {

subscript(bounds: CountablePartialRangeFrom<Int>) -> Substring {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(endIndex, offsetBy: -1)
return self[start ... end]
}
subscript (bounds: PartialRangeThrough<Int>) -> Substring {

subscript(bounds: PartialRangeThrough<Int>) -> Substring {
let end = index(startIndex, offsetBy: bounds.upperBound)
return self[startIndex ... end]
}
subscript (bounds: PartialRangeUpTo<Int>) -> Substring {

subscript(bounds: PartialRangeUpTo<Int>) -> Substring {
let end = index(startIndex, offsetBy: bounds.upperBound)
return self[startIndex ..< end]
}
Expand Down
5 changes: 0 additions & 5 deletions APFancyPagerDemo/APFancyPagerDemo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:.
}


}

15 changes: 6 additions & 9 deletions APFancyPagerDemo/APFancyPagerDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// Copyright © 2018 Apps. All rights reserved.
//

import UIKit
import APFancyPager
import UIKit

class ViewController: APFancyPagerViewController, APFancyPagerDelegate, APFancyPagerDataSource {
override func viewDidLoad() {
Expand All @@ -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
}
Expand Down Expand Up @@ -111,7 +111,4 @@ class ViewController: APFancyPagerViewController, APFancyPagerDelegate, APFancyP
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

Loading

0 comments on commit 7109e1b

Please sign in to comment.