Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Progressive Tab Scrolling Representation #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

@property (weak, nonatomic) id<GUITabPagerDataSource> dataSource;
@property (weak, nonatomic) id<GUITabPagerDelegate> delegate;
@property (assign, nonatomic) BOOL isProgressive;

- (void)reloadData;
- (NSInteger)selectedIndex;
Expand Down
54 changes: 46 additions & 8 deletions GUITabPagerViewController/Classes/GUITabPagerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
#import "GUITabPagerViewController.h"
#import "GUITabScrollView.h"

@interface GUITabPagerViewController () <GUITabScrollDelegate, UIPageViewControllerDataSource, UIPageViewControllerDelegate>
@interface GUITabPagerViewController () <GUITabScrollDelegate, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate>
{
BOOL tapped;
}

@property (strong, nonatomic) UIPageViewController *pageViewController;
@property (strong, nonatomic) GUITabScrollView *header;
Expand Down Expand Up @@ -37,9 +40,13 @@ - (void)viewDidLoad {
if ([view isKindOfClass:[UIScrollView class]]) {
[(UIScrollView *)view setCanCancelContentTouches:YES];
[(UIScrollView *)view setDelaysContentTouches:NO];
[(UIScrollView *)view setDelegate:self];
}
}

tapped = false;
self.isProgressive = YES;

[[self pageViewController] setDataSource:self];
[[self pageViewController] setDelegate:self];

Expand All @@ -50,6 +57,7 @@ - (void)viewDidLoad {

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self reloadTabs];
[self selectTabbarIndex:self.selectedIndex];
}

- (void)didReceiveMemoryWarning {
Expand All @@ -72,12 +80,14 @@ - (UIViewController *)pageViewController:(UIPageViewController *)pageViewControl
#pragma mark - Page View Delegate

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers {
NSInteger index = [[self viewControllers] indexOfObject:pendingViewControllers[0]];
[[self header] animateToTabAtIndex:index];

if ([[self delegate] respondsToSelector:@selector(tabPager:willTransitionToTabAtIndex:)]) {
[[self delegate] tabPager:self willTransitionToTabAtIndex:index];
}
if (!self.isProgressive) {
NSInteger index = [[self viewControllers] indexOfObject:pendingViewControllers[0]];
[[self header] animateToTabAtIndex:index];

if ([[self delegate] respondsToSelector:@selector(tabPager:willTransitionToTabAtIndex:)]) {
[[self delegate] tabPager:self willTransitionToTabAtIndex:index];
}
}
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
Expand All @@ -96,7 +106,7 @@ - (void)tabScrollView:(GUITabScrollView *)tabScrollView didSelectTabAtIndex:(NSI
if ([[self delegate] respondsToSelector:@selector(tabPager:willTransitionToTabAtIndex:)]) {
[[self delegate] tabPager:self willTransitionToTabAtIndex:index];
}

tapped = true;
[[self pageViewController] setViewControllers:@[[self viewControllers][index]]
direction:(index > [self selectedIndex]) ? UIPageViewControllerNavigationDirectionForward : UIPageViewControllerNavigationDirectionReverse
animated:YES
Expand Down Expand Up @@ -217,6 +227,34 @@ - (void)reloadTabs {
[[self view] addSubview:[self header]];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (self.isProgressive) {
CGPoint offset = scrollView.contentOffset;
float progress = 0;
NSInteger fromIndex = self.selectedIndex;
NSInteger toIndex = -1;
progress = (offset.x - self.view.bounds.size.width) / self.view.bounds.size.width;
if (progress > 0) {
if (fromIndex < [[self viewControllers] count] - 1) {
toIndex = fromIndex + 1;
}
}
else {
if (fromIndex > 0) {
toIndex = fromIndex - 1;
}
}
if (!tapped) {
[[self header] animateFromTabAtIndex:fromIndex toTabAtIndex:toIndex withProgress:progress];
}
else if (fabs(progress) >= 0.999999 || fabs(progress) <= 0.000001)
tapped = false;
}
}

#pragma mark - Public Methods

- (void)selectTabbarIndex:(NSInteger)index {
Expand Down
2 changes: 2 additions & 0 deletions GUITabPagerViewController/Classes/GUITabScrollView.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

- (void)animateToTabAtIndex:(NSInteger)index;
- (void)animateToTabAtIndex:(NSInteger)index animated:(BOOL)animated;
- (void)animateFromTabAtIndex:(NSInteger)fromIndex toTabAtIndex:(NSInteger)toIndex withProgress:(float)progress;


@end

Expand Down
27 changes: 26 additions & 1 deletion GUITabPagerViewController/Classes/GUITabScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,36 @@ - (void)animateToTabAtIndex:(NSInteger)index animated:(BOOL)animated {
}];
}

- (void)animateFromTabAtIndex:(NSInteger)fromIndex toTabAtIndex:(NSInteger)toIndex withProgress:(float)progress {
if (fromIndex != -1 && toIndex != -1) {
CGFloat x = [[self tabViews][0] frame].origin.x - 5;
CGFloat w = [[self tabViews][fromIndex] frame].size.width + 10;
for (int i = 0; i < fromIndex; i++) {
x += [[self tabViews][i] frame].size.width + 10;
}
CGFloat p = x - (self.frame.size.width - w) / 2;
x += fabs([[self tabViews][toIndex] frame].origin.x - [[self tabViews][fromIndex] frame].origin.x) * progress;


w += ([[self tabViews][toIndex] frame].size.width - [[self tabViews][fromIndex] frame].size.width) * fabs(progress);


CGFloat min = 0;
CGFloat max = MAX(0, self.contentSize.width - self.frame.size.width);

[self setContentOffset:CGPointMake(MAP(p, min, max), 0)];
[[self tabIndicatorDisplacement] setConstant:x];
[[self tabIndicatorWidth] setConstant:w];
[self layoutIfNeeded];

}
}

- (void)tabTapHandler:(UITapGestureRecognizer *)gestureRecognizer {
if ([[self tabScrollDelegate] respondsToSelector:@selector(tabScrollView:didSelectTabAtIndex:)]) {
NSInteger index = [[gestureRecognizer view] tag];
[[self tabScrollDelegate] tabScrollView:self didSelectTabAtIndex:index];
[self animateToTabAtIndex:index];
[[self tabScrollDelegate] tabScrollView:self didSelectTabAtIndex:index];
}
}

Expand Down