Skip to content

Commit

Permalink
- fixed tap timing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Niklas Fahl committed Jan 30, 2015
1 parent afac27c commit f316395
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
27 changes: 15 additions & 12 deletions Classes/CAPSPageMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
import UIKit

@objc protocol CAPSPageMenuDelegate {
// MARK: - Delegate functions

optional func willMoveToPage(controller: UIViewController, index: Int)
optional func didMoveToPage(controller: UIViewController, index: Int)
}

class MenuItemView: UIView {
// MARK: - Menu item view

var titleLabel : UILabel?
var menuItemSeparator : UIView?
Expand Down Expand Up @@ -109,6 +112,8 @@ class CAPSPageMenu: UIViewController, UIScrollViewDelegate, UIGestureRecognizerD

var delegate : CAPSPageMenuDelegate?

var tapTimer : NSTimer?

// MARK: - View life cycle

/**
Expand Down Expand Up @@ -269,10 +274,9 @@ class CAPSPageMenu: UIViewController, UIScrollViewDelegate, UIGestureRecognizerD
if controller.isKindOfClass(UIViewController) {
if index == 0.0 {
// Add first two controllers to scrollview and as child view controller
self.addChildViewController(controller as UIViewController)
(controller as UIViewController).view.frame = CGRectMake(self.view.frame.width * index, menuHeight, self.view.frame.width, self.view.frame.height - menuHeight)
controllerScrollView.addSubview((controller as UIViewController).view)
self.didMoveToParentViewController(self)
(controller as UIViewController).viewWillAppear(true)
addPageAtIndex(0)
(controller as UIViewController).viewDidAppear(true)
}

// Set up menu item for menu scroll view
Expand Down Expand Up @@ -503,14 +507,6 @@ class CAPSPageMenu: UIViewController, UIScrollViewDelegate, UIGestureRecognizerD
menuScrollView.setContentOffset(offset, animated: false)
}
}

// If user tapped item everything but current page need to be removed
if didTapMenuItemToScroll {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(scrollAnimationDurationOnMenuItemTap) * Int64(NSEC_PER_MSEC))
dispatch_after(time, dispatch_get_main_queue(), {
self.scrollViewDidEndTapScrollingAnimation()
})
}
}
} else {
didLayoutSubviewsAfterRotation = false
Expand Down Expand Up @@ -676,6 +672,13 @@ class CAPSPageMenu: UIViewController, UIScrollViewDelegate, UIGestureRecognizerD
var xOffset : CGFloat = CGFloat(itemIndex) * self.controllerScrollView.frame.width
self.controllerScrollView.setContentOffset(CGPoint(x: xOffset, y: self.controllerScrollView.contentOffset.y), animated: false)
})

if tapTimer != nil {
tapTimer!.invalidate()
}

var timerInterval : NSTimeInterval = Double(scrollAnimationDurationOnMenuItemTap) * 0.001
tapTimer = NSTimer.scheduledTimerWithTimeInterval(timerInterval, target: self, selector: "scrollViewDidEndTapScrollingAnimation", userInfo: nil, repeats: false)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TestTableViewController: UITableViewController {
super.viewDidAppear(animated)
self.tableView.showsVerticalScrollIndicator = true

// println("favorites page: viewDidAppear")
println("favorites page: viewDidAppear")
}

override func didReceiveMemoryWarning() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ class ViewController: UIViewController, CAPSPageMenuDelegate {

// Uncomment below for some navbar color animation fun using the new delegate functions

// func didMoveToPage(controller: UIViewController, index: Int) {
func didMoveToPage(controller: UIViewController, index: Int) {
println("did move to page")

// var color : UIColor = UIColor(red: 18.0/255.0, green: 150.0/255.0, blue: 225.0/255.0, alpha: 1.0)
// var navColor : UIColor = UIColor(red: 17.0/255.0, green: 64.0/255.0, blue: 107.0/255.0, alpha: 1.0)
//
Expand All @@ -87,9 +89,11 @@ class ViewController: UIViewController, CAPSPageMenuDelegate {
// }) { (completed) -> Void in
// println("did fade")
// }
// }
//
// func willMoveToPage(controller: UIViewController, index: Int) {
}

func willMoveToPage(controller: UIViewController, index: Int) {
println("will move to page")

// var color : UIColor = UIColor(red: 18.0/255.0, green: 150.0/255.0, blue: 225.0/255.0, alpha: 1.0)
// var navColor : UIColor = UIColor(red: 17.0/255.0, green: 64.0/255.0, blue: 107.0/255.0, alpha: 1.0)
//
Expand All @@ -109,5 +113,5 @@ class ViewController: UIViewController, CAPSPageMenuDelegate {
// }) { (completed) -> Void in
// println("did fade")
// }
// }
}
}

0 comments on commit f316395

Please sign in to comment.