From d668f96bc61f6cc9bcf9b93ea39d794a704738b0 Mon Sep 17 00:00:00 2001 From: Niklas Fahl Date: Thu, 12 Feb 2015 09:18:20 -0600 Subject: [PATCH 1/2] - added customization option for default starting page index --- Classes/CAPSPageMenu.swift | 32 +++++++++++++++---- .../ViewController.swift | 1 + 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Classes/CAPSPageMenu.swift b/Classes/CAPSPageMenu.swift index 0509f8c6..a6dee8ee 100644 --- a/Classes/CAPSPageMenu.swift +++ b/Classes/CAPSPageMenu.swift @@ -74,6 +74,7 @@ public class CAPSPageMenu: UIViewController, UIScrollViewDelegate, UIGestureReco var totalMenuItemWidthIfDifferentWidths : CGFloat = 0.0 public var scrollAnimationDurationOnMenuItemTap : Int = 500 // Millisecons var startingMenuMargin : CGFloat = 0.0 + var startingPageIndex : Int = 0 var selectionIndicatorView : UIView = UIView() @@ -179,9 +180,16 @@ public class CAPSPageMenu: UIViewController, UIScrollViewDelegate, UIGestureReco centerMenuItems = options![key] as Bool } else if key == "hideTopMenuBar" { hideTopMenuBar = options![key] as Bool + } else if key == "startingPageIndex" { + startingPageIndex = options![key] as Int } } + // Set starting page values based on starting page index that is set + currentPageIndex = startingPageIndex + lastPageIndex = startingPageIndex + startingPageForScroll = startingPageIndex + if hideTopMenuBar { addBottomMenuHairline = false menuHeight = 0.0 @@ -295,10 +303,10 @@ public class CAPSPageMenu: UIViewController, UIScrollViewDelegate, UIGestureReco for controller in controllerArray { if controller.isKindOfClass(UIViewController) { - if index == 0.0 { + if index == CGFloat(startingPageIndex) { // Add first two controllers to scrollview and as child view controller (controller as UIViewController).viewWillAppear(true) - addPageAtIndex(0) + addPageAtIndex(startingPageIndex) (controller as UIViewController).viewDidAppear(true) } @@ -369,6 +377,10 @@ public class CAPSPageMenu: UIViewController, UIScrollViewDelegate, UIGestureReco } } + if startingPageIndex > 0 && startingPageIndex < controllerArray.count { + controllerScrollView.setContentOffset(CGPoint(x: self.view.frame.width * CGFloat(startingPageIndex), y: self.controllerScrollView.contentOffset.y), animated: false) + } + // Set new content size for menu scroll view if needed if menuItemWidthBasedOnTitleTextWidth { menuScrollView.contentSize = CGSizeMake((totalMenuItemWidthIfDifferentWidths + menuMargin) + CGFloat(controllerArray.count) * menuMargin, menuHeight) @@ -385,14 +397,22 @@ public class CAPSPageMenu: UIViewController, UIScrollViewDelegate, UIGestureReco var selectionIndicatorFrame : CGRect = CGRect() if useMenuLikeSegmentedControl { - selectionIndicatorFrame = CGRectMake(0.0, menuHeight - selectionIndicatorHeight, self.view.frame.width / CGFloat(controllerArray.count), selectionIndicatorHeight) + selectionIndicatorFrame = CGRectMake(CGFloat(startingPageIndex) * self.view.frame.width / CGFloat(controllerArray.count), menuHeight - selectionIndicatorHeight, self.view.frame.width / CGFloat(controllerArray.count), selectionIndicatorHeight) } else if menuItemWidthBasedOnTitleTextWidth { - selectionIndicatorFrame = CGRectMake(menuMargin, menuHeight - selectionIndicatorHeight, menuItemWidths[0], selectionIndicatorHeight) + var startingPageMenuOffset : CGFloat = 0.0 + + for i in 0...startingPageIndex { + startingPageMenuOffset += menuItemWidths[i] + } + + selectionIndicatorFrame = CGRectMake(menuMargin + startingPageMenuOffset, menuHeight - selectionIndicatorHeight, menuItemWidths[startingPageIndex], selectionIndicatorHeight) } else { + var startingPageMenuOffset : CGFloat = (menuItemWidth + menuMargin) * CGFloat(startingPageIndex) + if centerMenuItems { - selectionIndicatorFrame = CGRectMake(startingMenuMargin + menuMargin, menuHeight - selectionIndicatorHeight, menuItemWidth, selectionIndicatorHeight) + selectionIndicatorFrame = CGRectMake(startingMenuMargin + menuMargin + startingPageMenuOffset, menuHeight - selectionIndicatorHeight, menuItemWidth, selectionIndicatorHeight) } else { - selectionIndicatorFrame = CGRectMake(menuMargin, menuHeight - selectionIndicatorHeight, menuItemWidth, selectionIndicatorHeight) + selectionIndicatorFrame = CGRectMake(menuMargin + startingPageMenuOffset, menuHeight - selectionIndicatorHeight, menuItemWidth, selectionIndicatorHeight) } } diff --git a/Demos/Demo 5/PageMenuDemoSegmentedControl/PageMenuDemoSegmentedControl/ViewController.swift b/Demos/Demo 5/PageMenuDemoSegmentedControl/PageMenuDemoSegmentedControl/ViewController.swift index 8ab89a24..b212852f 100644 --- a/Demos/Demo 5/PageMenuDemoSegmentedControl/PageMenuDemoSegmentedControl/ViewController.swift +++ b/Demos/Demo 5/PageMenuDemoSegmentedControl/PageMenuDemoSegmentedControl/ViewController.swift @@ -57,6 +57,7 @@ class ViewController: UIViewController, CAPSPageMenuDelegate { "useMenuLikeSegmentedControl": true, "menuItemSeparatorRoundEdges": true, "selectionIndicatorHeight": 2.0, + "startingPageIndex": 1, "menuItemSeparatorPercentageHeight": 0.1] // Initialize scroll menu From 2c65fa86026cf07ecef953cd4042d2c818ea2af9 Mon Sep 17 00:00:00 2001 From: Niklas Fahl Date: Tue, 17 Feb 2015 16:02:11 -0600 Subject: [PATCH 2/2] - switch statement in init --- Classes/CAPSPageMenu.swift | 53 ++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/Classes/CAPSPageMenu.swift b/Classes/CAPSPageMenu.swift index a6dee8ee..106a9395 100644 --- a/Classes/CAPSPageMenu.swift +++ b/Classes/CAPSPageMenu.swift @@ -74,7 +74,7 @@ public class CAPSPageMenu: UIViewController, UIScrollViewDelegate, UIGestureReco var totalMenuItemWidthIfDifferentWidths : CGFloat = 0.0 public var scrollAnimationDurationOnMenuItemTap : Int = 500 // Millisecons var startingMenuMargin : CGFloat = 0.0 - var startingPageIndex : Int = 0 + public var startingPageIndex : Int = 0 var selectionIndicatorView : UIView = UIView() @@ -136,52 +136,55 @@ public class CAPSPageMenu: UIViewController, UIScrollViewDelegate, UIGestureReco if options != nil { for key : String in options!.keys { - if key == "selectionIndicatorHeight" { + switch key { + case "selectionIndicatorHeight": selectionIndicatorHeight = options![key] as CGFloat - } else if key == "menuItemSeparatorWidth" { + case "menuItemSeparatorWidth": menuItemSeparatorWidth = options![key] as CGFloat - } else if key == "scrollMenuBackgroundColor" { + case "scrollMenuBackgroundColor": scrollMenuBackgroundColor = options![key] as UIColor - } else if key == "viewBackgroundColor" { + case "viewBackgroundColor": viewBackgroundColor = options![key] as UIColor - } else if key == "bottomMenuHairlineColor" { + case "bottomMenuHairlineColor": bottomMenuHairlineColor = options![key] as UIColor - } else if key == "selectionIndicatorColor" { + case "selectionIndicatorColor": selectionIndicatorColor = options![key] as UIColor - } else if key == "menuItemSeparatorColor" { + case "menuItemSeparatorColor": menuItemSeparatorColor = options![key] as UIColor - } else if key == "menuMargin" { + case "menuMargin": menuMargin = options![key] as CGFloat - } else if key == "menuHeight" { + case "menuHeight": menuHeight = options![key] as CGFloat - } else if key == "selectedMenuItemLabelColor" { + case "selectedMenuItemLabelColor": selectedMenuItemLabelColor = options![key] as UIColor - } else if key == "unselectedMenuItemLabelColor" { + case "unselectedMenuItemLabelColor": unselectedMenuItemLabelColor = options![key] as UIColor - } else if key == "useMenuLikeSegmentedControl" { + case "useMenuLikeSegmentedControl": useMenuLikeSegmentedControl = options![key] as Bool - } else if key == "menuItemSeparatorRoundEdges" { + case "menuItemSeparatorRoundEdges": menuItemSeparatorRoundEdges = options![key] as Bool - } else if key == "menuItemFont" { + case "menuItemFont": menuItemFont = options![key] as UIFont - } else if key == "menuItemSeparatorPercentageHeight" { + case "menuItemSeparatorPercentageHeight": menuItemSeparatorPercentageHeight = options![key] as CGFloat - } else if key == "menuItemWidth" { + case "menuItemWidth": menuItemWidth = options![key] as CGFloat - } else if key == "enableHorizontalBounce" { + case "enableHorizontalBounce": enableHorizontalBounce = options![key] as Bool - } else if key == "addBottomMenuHairline" { + case "addBottomMenuHairline": addBottomMenuHairline = options![key] as Bool - } else if key == "menuItemWidthBasedOnTitleTextWidth" { + case "menuItemWidthBasedOnTitleTextWidth": menuItemWidthBasedOnTitleTextWidth = options![key] as Bool - } else if key == "scrollAnimationDurationOnMenuItemTap" { + case "scrollAnimationDurationOnMenuItemTap": scrollAnimationDurationOnMenuItemTap = options![key] as Int - } else if key == "centerMenuItems" { + case "centerMenuItems": centerMenuItems = options![key] as Bool - } else if key == "hideTopMenuBar" { + case "hideTopMenuBar": hideTopMenuBar = options![key] as Bool - } else if key == "startingPageIndex" { - startingPageIndex = options![key] as Int + case "hideTopMenuBar": + hideTopMenuBar = options![key] as Bool + default: + println("default case") } }