diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..35cfb4d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+.DS_Store
+xcuserdata
+project.xcworkspace
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..e6ea072
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,10 @@
+language: objective-c
+osx_image: beta-xcode6.3
+
+before_install:
+ - brew update
+ - if brew outdated | grep -qx xctool; then brew upgrade xctool; fi
+
+script:
+ - xctool clean build -project Demos/TwitterLike/TwitterLike.xcodeproj -scheme TwitterLike -sdk iphonesimulator
+ - xctool clean build -project Demos/TinderLike/TinderLike.xcodeproj -scheme TinderLike -sdk iphonesimulator
diff --git a/Demos/.DS_Store b/Demos/.DS_Store
deleted file mode 100644
index 5008ddf..0000000
Binary files a/Demos/.DS_Store and /dev/null differ
diff --git a/Demos/TinderLike/.DS_Store b/Demos/TinderLike/.DS_Store
deleted file mode 100644
index 9ee1c77..0000000
Binary files a/Demos/TinderLike/.DS_Store and /dev/null differ
diff --git a/Demos/TinderLike/TinderLike.xcodeproj/xcshareddata/xcschemes/TinderLike.xcscheme b/Demos/TinderLike/TinderLike.xcodeproj/xcshareddata/xcschemes/TinderLike.xcscheme
new file mode 100644
index 0000000..819bb99
--- /dev/null
+++ b/Demos/TinderLike/TinderLike.xcodeproj/xcshareddata/xcschemes/TinderLike.xcscheme
@@ -0,0 +1,112 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Demos/TinderLike/TinderLike/AppDelegate.swift b/Demos/TinderLike/TinderLike/AppDelegate.swift
index 714797c..a9e405f 100644
--- a/Demos/TinderLike/TinderLike/AppDelegate.swift
+++ b/Demos/TinderLike/TinderLike/AppDelegate.swift
@@ -13,12 +13,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var nav: UINavigationController?
- var controller: SLPagingViewSwift?
+ var controller: SLPagingViewSwift!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
- var orange = UIColor(red: 255/255, green: 69.0/255, blue: 0.0/255, alpha: 1.0)
- var gray = UIColor(red: 0.84, green: 0.84, blue: 0.84, alpha: 1.0)
+ let orange = UIColor(red: 255/255, green: 69.0/255, blue: 0.0/255, alpha: 1.0)
+ let gray = UIColor(red: 0.84, green: 0.84, blue: 0.84, alpha: 1.0)
var ctr1 = UIViewController()
ctr1.title = "Ctr1"
@@ -42,25 +42,27 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var controllers = [ctr1, ctr2, ctr3]
controller = SLPagingViewSwift(items: items, controllers: controllers, showPageControl: false)
- controller?.pagingViewMoving = ({ subviews in
- for v in subviews {
- var lbl = v as UIImageView
- var c = gray
-
- if(lbl.frame.origin.x > 45 && lbl.frame.origin.x < 145) {
- c = self.gradient(Double(lbl.frame.origin.x), topX: Double(46), bottomX: Double(144), initC: orange, goal: gray)
+ controller.pagingViewMoving = ({ subviews in
+ if let imageViews = subviews as? [UIImageView] {
+ for imgView in imageViews {
+ var c = gray
+ let originX = Double(imgView.frame.origin.x)
+
+ if (originX > 45 && originX < 145) {
+ c = self.gradient(originX, topX: 46, bottomX: 144, initC: orange, goal: gray)
+ }
+ else if (originX > 145 && originX < 245) {
+ c = self.gradient(originX, topX: 146, bottomX: 244, initC: gray, goal: orange)
+ }
+ else if(originX == 145){
+ c = orange
+ }
+ imgView.tintColor = c
}
- else if (lbl.frame.origin.x > 145 && lbl.frame.origin.x < 245) {
- c = self.gradient(Double(lbl.frame.origin.x), topX: Double(146), bottomX: Double(244), initC: gray, goal: orange)
- }
- else if(lbl.frame.origin.x == 145){
- c = orange
- }
- lbl.tintColor = c
}
})
- self.nav = UINavigationController(rootViewController: self.controller!)
+ self.nav = UINavigationController(rootViewController: self.controller)
self.window?.rootViewController = self.nav
self.window?.backgroundColor = UIColor.whiteColor()
self.window?.makeKeyAndVisible()
diff --git a/Demos/TwitterLike/.DS_Store b/Demos/TwitterLike/.DS_Store
deleted file mode 100644
index 1f3033c..0000000
Binary files a/Demos/TwitterLike/.DS_Store and /dev/null differ
diff --git a/Demos/TwitterLike/TwitterLike.xcodeproj/xcshareddata/xcschemes/TwitterLike.xcscheme b/Demos/TwitterLike/TwitterLike.xcodeproj/xcshareddata/xcschemes/TwitterLike.xcscheme
new file mode 100644
index 0000000..511bbda
--- /dev/null
+++ b/Demos/TwitterLike/TwitterLike.xcodeproj/xcshareddata/xcschemes/TwitterLike.xcscheme
@@ -0,0 +1,112 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Demos/TwitterLike/TwitterLike/AppDelegate.swift b/Demos/TwitterLike/TwitterLike/AppDelegate.swift
index f38c29b..d9eea6c 100644
--- a/Demos/TwitterLike/TwitterLike/AppDelegate.swift
+++ b/Demos/TwitterLike/TwitterLike/AppDelegate.swift
@@ -12,18 +12,18 @@ import UIKit
class AppDelegate: UIResponder, UIApplicationDelegate, UITableViewDelegate, UITableViewDataSource {
var window: UIWindow?
- var nav: UINavigationController?
- var controller: SLPagingViewSwift?
- var dataSource: NSMutableArray?
+ var nav: UINavigationController!
+ var controller: SLPagingViewSwift!
+ var dataSource: [String]!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
- self.dataSource = NSMutableArray(array: ["Hello world!", "Shaqtin' a fool!", "YEAHHH!",
+ self.dataSource = ["Hello world!", "Shaqtin' a fool!", "YEAHHH!",
"Hello world!", "Shaqtin' a fool!", "YEAHHH!",
"Hello world!", "Shaqtin' a fool!", "YEAHHH!",
"Hello world!", "Shaqtin' a fool!", "YEAHHH!",
- "Hello world!", "Shaqtin' a fool!", "YEAHHH!"])
+ "Hello world!", "Shaqtin' a fool!", "YEAHHH!"]
var navTitleLabel1 = UILabel()
navTitleLabel1.text = "Home"
@@ -40,35 +40,36 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UITableViewDelegate, UITa
controller = SLPagingViewSwift(items: [navTitleLabel1, navTitleLabel2, navTitleLabel3], views: [self.tableView(), self.tableView(), self.tableView()], showPageControl: true, navBarBackground: UIColor(red: 0.33, green: 0.68, blue: 0.91, alpha: 1.0))
- controller?.currentPageControlColor = UIColor.whiteColor()
- controller?.tintPageControlColor = UIColor(white: 0.799, alpha: 1.0)
- controller?.pagingViewMovingRedefine = ({ scrollView, subviews in
+ controller.currentPageControlColor = UIColor.whiteColor()
+ controller.tintPageControlColor = UIColor(white: 0.799, alpha: 1.0)
+ controller.pagingViewMovingRedefine = ({ scrollView, subviews in
var i = 0
- var xOffset = scrollView.contentOffset.x
- for v in subviews {
- var lbl = v as UILabel
- var alpha = CGFloat(0)
-
- if(lbl.frame.origin.x > 45 && lbl.frame.origin.x < 145) {
- alpha = 1.0 - (xOffset - (CGFloat(i)*320.0)) / 320.0
- }
- else if (lbl.frame.origin.x > 145 && lbl.frame.origin.x < 245) {
- alpha = (xOffset - (CGFloat(i)*320.0)) / 320.0 + 1.0
- }
- else if(lbl.frame.origin.x == 145){
- alpha = 1.0
+ let xOffset = scrollView.contentOffset.x
+ if let lbls = subviews as? [UILabel] {
+ for lbl in lbls {
+ var alpha : CGFloat = 0
+
+ if(lbl.frame.origin.x > 45 && lbl.frame.origin.x < 145) {
+ alpha = 1.0 - (xOffset - (CGFloat(i)*320.0)) / 320.0
+ }
+ else if (lbl.frame.origin.x > 145 && lbl.frame.origin.x < 245) {
+ alpha = (xOffset - (CGFloat(i)*320.0)) / 320.0 + 1.0
+ }
+ else if(lbl.frame.origin.x == 145){
+ alpha = 1.0
+ }
+ lbl.alpha = alpha
+ i++
}
- lbl.alpha = CGFloat(alpha)
- i++
}
})
- controller?.didChangedPage = ({ currentIndex in
+ controller.didChangedPage = ({ currentIndex in
println(currentIndex)
})
- self.nav = UINavigationController(rootViewController: self.controller!)
+ self.nav = UINavigationController(rootViewController: self.controller)
self.window?.rootViewController = self.nav
self.window?.backgroundColor = UIColor.whiteColor()
self.window?.makeKeyAndVisible()
@@ -115,7 +116,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UITableViewDelegate, UITa
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return self.dataSource!.count
+ return self.dataSource.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
@@ -129,8 +130,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UITableViewDelegate, UITa
cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentifier)
cell?.textLabel?.numberOfLines = 0
}
- cell!.imageView?.image = UIImage(named: NSString(format: "avatar_%d.jpg", indexPath.row % 3))
- cell!.textLabel!.text = self.dataSource?.objectAtIndex(indexPath.row) as String
+ cell!.imageView?.image = UIImage(named: "avatar_\(indexPath.row % 3).png")
+ cell!.textLabel!.text = self.dataSource[indexPath.row]
return cell!
}
diff --git a/README.md b/README.md
index 0474d09..e317f3e 100644
--- a/README.md
+++ b/README.md
@@ -9,8 +9,9 @@ A navigation bar system allowing to do a Tinder like or Twitter like. SLPagingVi
## Requirements
-* iOS 7.0+
+* iOS 7.0+
* ARC
+* Swift 1.2
## Installation
@@ -47,7 +48,7 @@ Easy to implement:
img2 = img2?.imageWithRenderingMode(.AlwaysTemplate)
var img3 = UIImage(named: "chat")
img3 = img3?.imageWithRenderingMode(.AlwaysTemplate)
-
+
var items = [UIImageView(image: img1), UIImageView(image: img2), UIImageView(image: img3)]
var controllers = [ctr1, ctr2, ctr3]
controller = SLPagingViewSwift(items: items, controllers: controllers, showPageControl: false)
@@ -63,7 +64,7 @@ Then you can make your own behaviors:
for v in subviews {
var lbl = v as UIImageView
var c = gray
-
+
if(lbl.frame.origin.x > 45 && lbl.frame.origin.x < 145) {
c = self.gradient(Double(lbl.frame.origin.x), topX: Double(46), bottomX: Double(144), initC: orange, goal: gray)
}
@@ -92,7 +93,7 @@ Twitter like behaviors
for v in subviews {
var lbl = v as UILabel
var alpha = CGFloat(0)
-
+
if(lbl.frame.origin.x > 45 && lbl.frame.origin.x < 145) {
alpha = 1.0 - (xOffset - (CGFloat(i)*320.0)) / 320.0
}
@@ -116,7 +117,7 @@ If you want to changed the default page control index (or whatever) you can do i
```swift
- func setCurrentIndex(index: NSInteger, animated: Bool)
+ func setCurrentIndex(index: Int, animated: Bool)
````
###Navigation items style
@@ -149,4 +150,4 @@ By using one of these values:
##License
-Available under MIT license, please read LICENSE for more informations.
\ No newline at end of file
+Available under MIT license, please read LICENSE for more informations.
diff --git a/SLPagingViewSwift/SLPagingViewSwift.swift b/SLPagingViewSwift/SLPagingViewSwift.swift
index ff35af4..7164355 100644
--- a/SLPagingViewSwift/SLPagingViewSwift.swift
+++ b/SLPagingViewSwift/SLPagingViewSwift.swift
@@ -8,7 +8,7 @@
import UIKit
-enum SLNavigationSideItemsStyle: Int {
+public enum SLNavigationSideItemsStyle: Int {
case SLNavigationSideItemsStyleOnBounds = 40
case SLNavigationSideItemsStyleClose = 30
case SLNavigationSideItemsStyleNormal = 20
@@ -17,49 +17,49 @@ enum SLNavigationSideItemsStyle: Int {
case SLNavigationSideItemsStyleCloseToEachOne = -40
}
-typealias SLPagingViewMoving = ((subviews: NSArray)-> ())
-typealias SLPagingViewMovingRedefine = ((scrollView: UIScrollView, subviews: NSArray)-> ())
-typealias SLPagingViewDidChanged = ((currentPage: NSInteger)-> ())
+public typealias SLPagingViewMoving = ((subviews: [UIView])-> ())
+public typealias SLPagingViewMovingRedefine = ((scrollView: UIScrollView, subviews: NSArray)-> ())
+public typealias SLPagingViewDidChanged = ((currentPage: Int)-> ())
-class SLPagingViewSwift: UIViewController, UIScrollViewDelegate {
+public class SLPagingViewSwift: UIViewController, UIScrollViewDelegate {
// MARK: - Public properties
- var viewControllers: NSDictionary = NSDictionary()
- var currentPageControlColor: UIColor?
- var tintPageControlColor: UIColor?
- var pagingViewMoving: SLPagingViewMoving?
- var pagingViewMovingRedefine: SLPagingViewMovingRedefine?
- var didChangedPage: SLPagingViewDidChanged?
- var navigationSideItemsStyle: SLNavigationSideItemsStyle = .SLNavigationSideItemsStyleDefault
+ var views = [Int : UIView]()
+ public var currentPageControlColor: UIColor?
+ public var tintPageControlColor: UIColor?
+ public var pagingViewMoving: SLPagingViewMoving?
+ public var pagingViewMovingRedefine: SLPagingViewMovingRedefine?
+ public var didChangedPage: SLPagingViewDidChanged?
+ public var navigationSideItemsStyle: SLNavigationSideItemsStyle = .SLNavigationSideItemsStyleDefault
// MARK: - Private properties
private var SCREENSIZE: CGSize {
return UIScreen.mainScreen().bounds.size
}
- private var scrollView: UIScrollView = UIScrollView()
- private var pageControl: UIPageControl = UIPageControl()
+ private var scrollView: UIScrollView!
+ private var pageControl: UIPageControl!
private var navigationBarView: UIView = UIView()
- private var navItems: NSMutableArray = NSMutableArray()
+ private var navItems: [UIView] = []
private var needToShowPageControl: Bool = false
private var isUserInteraction: Bool = false
- private var indexSelected: NSInteger = 0
+ private var indexSelected: Int = 0
// MARK: - Constructors
- required init(coder decoder: NSCoder) {
+ public required init(coder decoder: NSCoder) {
super.init(coder: decoder)
}
- override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
+ public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// Here you can init your properties
}
// MARK: - Constructors with items & views
- convenience init(items: NSArray, views: NSArray) {
+ public convenience init(items: [UIView], views: [UIView]) {
self.init(items: items, views: views, showPageControl:false, navBarBackground:UIColor.whiteColor())
}
- convenience init(items: NSArray, views: NSArray, showPageControl: Bool){
+ public convenience init(items: [UIView], views: [UIView], showPageControl: Bool){
self.init(items: items, views: views, showPageControl:showPageControl, navBarBackground:UIColor.whiteColor())
}
@@ -73,61 +73,35 @@ class SLPagingViewSwift: UIViewController, UIScrollViewDelegate {
*
* @return Instance of SLPagingViewController
*/
- init(items: NSArray, views: NSArray, showPageControl: Bool, navBarBackground: UIColor){
- super.init()
+ public init(items: [UIView], views: [UIView], showPageControl: Bool, navBarBackground: UIColor) {
+ super.init(nibName: nil, bundle: nil)
needToShowPageControl = showPageControl
navigationBarView.backgroundColor = navBarBackground
isUserInteraction = true
- var i: Int = 0
- for item in items{
- if item.isKindOfClass(UIView.classForCoder()){
- var v = item as UIView
- var vSize: CGSize = v.isKindOfClass(UILabel.classForCoder()) ? self.getLabelSize(v as UILabel) : v.frame.size
- var originX = (self.SCREENSIZE.width/2.0 - vSize.width/2.0) + CGFloat(i * 100)
- v.frame = CGRectMake(originX, 8, vSize.width, vSize.height)
- v.tag = i
- var tap = UITapGestureRecognizer(target: self, action: "tapOnHeader:")
- v.addGestureRecognizer(tap)
- v.userInteractionEnabled = true
- self.navigationBarView.addSubview(v)
- self.navItems.addObject(v)
- i++
- }
+ for (i, v) in enumerate(items) {
+ let vSize: CGSize = (v as? UILabel)?._slpGetSize() ?? v.frame.size
+ let originX = (self.SCREENSIZE.width/2.0 - vSize.width/2.0) + CGFloat(i * 100)
+ v.frame = CGRectMake(originX, 8, vSize.width, vSize.height)
+ v.tag = i
+ let tap = UITapGestureRecognizer(target: self, action: "tapOnHeader:")
+ v.addGestureRecognizer(tap)
+ v.userInteractionEnabled = true
+ self.navigationBarView.addSubview(v)
+ self.navItems.append(v)
}
- if (views.count > 0){
- var controllerKeys = NSMutableArray()
- i = 0
- for controller in views{
- if controller.isKindOfClass(UIView.classForCoder()){
- var ctr = controller as UIView
- ctr.tag = i
- controllerKeys.addObject(NSNumber(integer: i))
- }
- else if controller.isKindOfClass(UIViewController.classForCoder()){
- var ctr = controller as UIViewController
- ctr.view.tag = i
- controllerKeys.addObject(NSNumber(integer: i))
- }
- i++
- }
-
- if controllerKeys.count == views.count {
- self.viewControllers = NSDictionary(objects: views, forKeys: controllerKeys)
- }
- else{
- var exc = NSException(name: "View Controllers error", reason: "Some objects in viewControllers are not kind of UIViewController!", userInfo: nil)
- exc.raise()
- }
+ for (i, view) in enumerate(views) {
+ view.tag = i
+ self.views[i] = view
}
}
// MARK: - Constructors with controllers
- convenience init(controllers: NSArray){
+ public convenience init(controllers: [UIViewController]){
self.init(controllers: controllers, showPageControl: true, navBarBackground: UIColor.whiteColor())
}
- convenience init(controllers: NSArray, showPageControl: Bool){
+ public convenience init(controllers: [UIViewController], showPageControl: Bool){
self.init(controllers: controllers, showPageControl: true, navBarBackground: UIColor.whiteColor())
}
@@ -142,25 +116,23 @@ class SLPagingViewSwift: UIViewController, UIScrollViewDelegate {
*
* @return Instance of SLPagingViewController
*/
- convenience init(controllers: NSArray, showPageControl: Bool, navBarBackground: UIColor){
- var views = NSMutableArray()
- var items = NSMutableArray()
+ public convenience init(controllers: [UIViewController], showPageControl: Bool, navBarBackground: UIColor){
+ var views = [UIView]()
+ var items = [UILabel]()
for ctr in controllers {
- if ctr.isKindOfClass(UIViewController.classForCoder()) {
- views.addObject(ctr)
- var item = UILabel()
- item.text = ctr.title
- items.addObject(item)
- }
+ let item = UILabel()
+ item.text = ctr.title
+ views.append(ctr.view)
+ items.append(item)
}
self.init(items: items, views: views, showPageControl:showPageControl, navBarBackground:navBarBackground)
}
// MARK: - Constructors with items & controllers
- convenience init(items: NSArray, controllers: NSArray){
+ public convenience init(items: [UIView], controllers: [UIViewController]){
self.init(items: items, controllers: controllers, showPageControl: true, navBarBackground: UIColor.whiteColor())
}
- convenience init(items: NSArray, controllers: NSArray, showPageControl: Bool){
+ public convenience init(items: [UIView], controllers: [UIViewController], showPageControl: Bool){
self.init(items: items, controllers: controllers, showPageControl: showPageControl, navBarBackground: UIColor.whiteColor())
}
@@ -174,30 +146,23 @@ class SLPagingViewSwift: UIViewController, UIScrollViewDelegate {
*
* @return Instance of SLPagingViewController
*/
- convenience init(items: NSArray, controllers: NSArray, showPageControl: Bool, navBarBackground: UIColor){
- var views = NSMutableArray()
+ public convenience init(items: [UIView], controllers: [UIViewController], showPageControl: Bool, navBarBackground: UIColor){
+ var views = [UIView]()
for ctr in controllers {
- if ctr.isKindOfClass(UIViewController.classForCoder()) {
- views.addObject(ctr.valueForKey("view")!)
- }
+ views.append(ctr.view)
}
self.init(items: items, views: views, showPageControl:showPageControl, navBarBackground:navBarBackground)
}
// MARK: - Life cycle
- override func viewDidLoad() {
+ public override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setupPagingProcess()
self.setCurrentIndex(self.indexSelected, animated: false)
}
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
-
- override func viewWillLayoutSubviews() {
+ public override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.navigationBarView.frame = CGRectMake(0, 0, self.SCREENSIZE.width, 44)
}
@@ -209,7 +174,7 @@ class SLPagingViewSwift: UIViewController, UIScrollViewDelegate {
*
* @param activate state you want to set to UserInteraction
*/
- func updateUserInteractionOnNavigation(active: Bool){
+ public func updateUserInteractionOnNavigation(active: Bool){
self.isUserInteraction = active
}
@@ -219,7 +184,7 @@ class SLPagingViewSwift: UIViewController, UIScrollViewDelegate {
* @param index of the wanted page
* @param animated animate the moving
*/
- func setCurrentIndex(index: NSInteger, animated: Bool){
+ public func setCurrentIndex(index: Int, animated: Bool){
// Be sure we got an existing index
if(index < 0 || index > self.navigationBarView.subviews.count-1){
var exc = NSException(name: "Index out of range", reason: "The index is out of range of subviews's countsd!", userInfo: nil)
@@ -227,7 +192,7 @@ class SLPagingViewSwift: UIViewController, UIScrollViewDelegate {
}
self.indexSelected = index
// Get the right position and update it
- var xOffset = CGFloat(index) * self.SCREENSIZE.width
+ let xOffset = CGFloat(index) * self.SCREENSIZE.width
self.scrollView.setContentOffset(CGPointMake(xOffset, self.scrollView.contentOffset.y), animated: animated)
}
@@ -269,34 +234,25 @@ class SLPagingViewSwift: UIViewController, UIScrollViewDelegate {
// Loads all views
private func addViews() {
- if self.viewControllers.count > 0 {
- let width = SCREENSIZE.width * CGFloat(self.viewControllers.count)
+ if self.views.count > 0 {
+ let width = SCREENSIZE.width * CGFloat(self.views.count)
let height = self.view.frame.height
self.scrollView.contentSize = CGSize(width: width, height: height)
var i: Int = 0
- self.viewControllers.enumerateKeysAndObjectsUsingBlock({ key, obj, stop in
- var rect: CGRect = CGRectMake(self.SCREENSIZE.width * CGFloat(i), 0, self.SCREENSIZE.width, self.SCREENSIZE.height)
- var v = obj as UIView
- v.frame = rect
+ while let v = views[i] {
+ v.frame = CGRectMake(self.SCREENSIZE.width * CGFloat(i), 0, self.SCREENSIZE.width, self.SCREENSIZE.height)
self.scrollView.addSubview(v)
i++
- })
+ }
}
}
- private func getLabelSize(lbl: UILabel) -> CGSize{
- var txt = lbl.text!
- return txt.sizeWithAttributes([NSFontAttributeName: lbl.font])
- }
-
private func sendNewIndex(scrollView: UIScrollView){
- var xOffset = Float(scrollView.contentOffset.x)
+ let xOffset = Float(scrollView.contentOffset.x)
var currentIndex = (Int(roundf(xOffset)) % (self.navigationBarView.subviews.count * Int(self.SCREENSIZE.width))) / Int(self.SCREENSIZE.width)
- if(self.pageControl.currentPage != currentIndex) {
+ if self.needToShowPageControl && self.pageControl.currentPage != currentIndex {
self.pageControl.currentPage = currentIndex
- if self.didChangedPage != nil {
- self.didChangedPage!(currentPage: currentIndex)
- }
+ self.didChangedPage?(currentPage: currentIndex)
}
}
@@ -309,40 +265,37 @@ class SLPagingViewSwift: UIViewController, UIScrollViewDelegate {
// Scroll to the view tapped
func tapOnHeader(recognizer: UITapGestureRecognizer){
- if(self.isUserInteraction){
- var key = recognizer.view?.tag
- var view = self.viewControllers.objectForKey(NSNumber(integer: key!)) as UIView
+ if let key = recognizer.view?.tag, view = self.views[key] where self.isUserInteraction {
self.scrollView.scrollRectToVisible(view.frame, animated: true)
}
}
// MARK: - UIScrollViewDelegate
- func scrollViewDidScroll(scrollView: UIScrollView) {
- var xOffset = scrollView.contentOffset.x
- var i = 0
- for obj in self.navItems {
- var v:UIView = obj as UIView
- var distance = 100 + self.navigationSideItemsStyle.rawValue
- var vSize = v.frame.size
- var originX = self.getOriginX(vSize, idx: CGFloat(i), distance: CGFloat(distance), xOffset: CGFloat(xOffset))
+ public func scrollViewDidScroll(scrollView: UIScrollView) {
+ let xOffset = scrollView.contentOffset.x
+ let distance = CGFloat(100 + self.navigationSideItemsStyle.rawValue)
+ for (i, v) in enumerate(self.navItems) {
+ let vSize = v.frame.size
+ let originX = self.getOriginX(vSize, idx: CGFloat(i), distance: CGFloat(distance), xOffset: xOffset)
v.frame = CGRectMake(originX, 8, vSize.width, vSize.height)
- i++
- }
- if (self.pagingViewMovingRedefine != nil) {
- self.pagingViewMovingRedefine!(scrollView: scrollView, subviews: self.navItems)
- }
- if (self.pagingViewMoving != nil) {
- self.pagingViewMoving!(subviews: self.navItems)
}
+ self.pagingViewMovingRedefine?(scrollView: scrollView, subviews: self.navItems)
+ self.pagingViewMoving?(subviews: self.navItems)
}
- func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
+ public func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
self.sendNewIndex(scrollView)
}
- func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {
+ public func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {
self.sendNewIndex(scrollView)
}
+}
+
+extension UILabel {
+ func _slpGetSize() -> CGSize? {
+ return (text as NSString?)?.sizeWithAttributes([NSFontAttributeName: font])
+ }
}
\ No newline at end of file