Skip to content

Commit

Permalink
Fix #22.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Barreto committed Aug 18, 2015
1 parent c433a07 commit 033b656
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 35 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ XLPagerTabStrip

By [XMARTLABS](http://xmartlabs.com).

[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/xmartlabs/XLPagerTabStrip/blob/master/LICENSE)
[![version](https://img.shields.io/badge/pod-2.0.0-blue.svg)](https://github.com/xmartlabs/XLPagerTabStrip/releases)

Android [PagerTabStrip](http://developer.android.com/reference/android/support/v4/view/PagerTabStrip.html) for iOS!

**XLPagerTabStrip** is a *Container View Controller* that allows us to switch easily among a collection of view controllers. Pan gesture can be used to move on to next or previous view controller. It shows a interactive indicator of the current, previous, next child view controllers.
Expand Down Expand Up @@ -40,7 +43,9 @@ FAQ

```objc
-(void)moveToViewControllerAtIndex:(NSUInteger)index;
-(void)moveToViewControllerAtIndex:(NSUInteger)index animated:(BOOL)animated;
-(void)moveToViewController:(UIViewController *)viewController;
-(void)moveToViewController:(UIViewController *)viewController animated:(BOOL)animated;
```

#####How to change the selected tab (XLButtonBarViewCell) look and feel based on the selected state
Expand All @@ -59,7 +64,7 @@ Installation
The easiest way to use `XLPagerTabStrip` in your app is via [CocoaPods](http://cocoapods.org/ "CocoaPods").

1. Add the following line in the project's Podfile file:
`pod 'XLPagerTabStrip', '~> 1.0'`.
`pod 'XLPagerTabStrip', '~> 2.0'`.
2. Run the command `pod install` from the Podfile folder directory.


Expand All @@ -85,7 +90,7 @@ Requirements
Release Notes
--------------

Version 2.0.0 (master)
Version 2.0.0

* Added ability to change look and feel of selected tab.
* `changeCurrentIndexProgressiveBlock` added to `XLButtonBarPagerTabStripViewController`.
Expand All @@ -96,6 +101,7 @@ Version 2.0.0 (master)
* Bug Fix Issue #54: Twitter PagerTabStrip wasn't loading the navigation title correctly.
* Bug Fix Issue #32: Demo for Nav Button Bar Example fix.
* Bug Fix Issue #32: Twitter Pager white dots that mark which tab is currently selected is non selectable now.
* Bug Fix Issue #22: moveToViewControllerAtIndex: in viewDidLoad or viewWillAppear is not reflected in buttonBarView.

Version 1.1.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,16 @@ - (void)viewDidLoad
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UICollectionViewLayoutAttributes *attributes = [self.buttonBarView layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0]];
CGRect cellRect = attributes.frame;
[self.buttonBarView.selectedBar setFrame:CGRectMake(cellRect.origin.x, self.buttonBarView.frame.size.height - self.buttonBarView.selectedBarHeight, cellRect.size.width, self.buttonBarView.selectedBarHeight)];
[self.buttonBarView layoutIfNeeded];
[self.buttonBarView moveToIndex:self.currentIndex animated:NO swipeDirection:XLPagerTabStripDirectionNone pagerScroll:(self.isProgressiveIndicator ? XLPagerScrollYES :XLPagerScrollOnlyIfOutOfScreen)];
}

-(void)reloadPagerTabStripView
{
[super reloadPagerTabStripView];
if ([self isViewLoaded]){
[self.buttonBarView reloadData];
[self.buttonBarView moveToIndex:self.currentIndex animated:NO swipeDirection:XLPagerTabStripDirectionNone];
[self.buttonBarView moveToIndex:self.currentIndex animated:NO swipeDirection:XLPagerTabStripDirectionNone pagerScroll:XLPagerScrollYES];
}
}

Expand Down Expand Up @@ -121,7 +120,7 @@ -(void)pagerTabStripViewController:(XLPagerTabStripViewController *)pagerTabStri
if (toIndex < fromIndex){
direction = XLPagerTabStripDirectionRight;
}
[self.buttonBarView moveToIndex:toIndex animated:YES swipeDirection:direction];
[self.buttonBarView moveToIndex:toIndex animated:YES swipeDirection:direction pagerScroll:XLPagerScrollYES];
if (self.changeCurrentIndexBlock) {
XLButtonBarViewCell *oldCell = (XLButtonBarViewCell*)[self.buttonBarView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex != fromIndex ? fromIndex : toIndex inSection:0]];
XLButtonBarViewCell *newCell = (XLButtonBarViewCell*)[self.buttonBarView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0]];
Expand All @@ -139,7 +138,7 @@ -(void)pagerTabStripViewController:(XLPagerTabStripViewController *)pagerTabStri
if (self.shouldUpdateButtonBarView){
[self.buttonBarView moveFromIndex:fromIndex
toIndex:toIndex
withProgressPercentage:progressPercentage];
withProgressPercentage:progressPercentage pagerScroll:XLPagerScrollYES];

if (self.changeCurrentIndexProgressiveBlock) {
XLButtonBarViewCell *oldCell = (XLButtonBarViewCell*)[self.buttonBarView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex != fromIndex ? fromIndex : toIndex inSection:0]];
Expand Down Expand Up @@ -172,7 +171,7 @@ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPa
if (indexPath.item == self.currentIndex)
return;

[self.buttonBarView moveToIndex:indexPath.item animated:YES swipeDirection:XLPagerTabStripDirectionNone];
[self.buttonBarView moveToIndex:indexPath.item animated:YES swipeDirection:XLPagerTabStripDirectionNone pagerScroll:XLPagerScrollYES];
self.shouldUpdateButtonBarView = NO;

XLButtonBarViewCell *oldCell = (XLButtonBarViewCell*)[self.buttonBarView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0]];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ typedef NS_ENUM(NSUInteger, XLPagerTabStripDirection) {
@property BOOL isElasticIndicatorLimit;

-(void)moveToViewControllerAtIndex:(NSUInteger)index;
-(void)moveToViewControllerAtIndex:(NSUInteger)index animated:(bool)animated;
-(void)moveToViewControllerAtIndex:(NSUInteger)index animated:(BOOL)animated;
-(void)moveToViewController:(UIViewController *)viewController;
-(void)moveToViewController:(UIViewController *)viewController animated:(bool)animated;
-(void)moveToViewController:(UIViewController *)viewController animated:(BOOL)animated;
-(void)reloadPagerTabStripView;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ -(void)moveToViewControllerAtIndex:(NSUInteger)index
}


-(void)moveToViewControllerAtIndex:(NSUInteger)index animated:(bool)animated
-(void)moveToViewControllerAtIndex:(NSUInteger)index animated:(BOOL)animated
{
if (![self isViewLoaded]){
if (!self.isViewLoaded || !self.view.window){
self.currentIndex = index;
}
else{
Expand Down Expand Up @@ -170,7 +170,7 @@ -(void)moveToViewController:(UIViewController *)viewController
[self moveToViewControllerAtIndex:[self.pagerTabStripChildViewControllers indexOfObject:viewController]];
}

-(void)moveToViewController:(UIViewController *)viewController animated:(bool)animated {
-(void)moveToViewController:(UIViewController *)viewController animated:(BOOL)animated {
[self moveToViewControllerAtIndex:[self.pagerTabStripChildViewControllers indexOfObject:viewController] animated:animated];
}

Expand Down
9 changes: 7 additions & 2 deletions XLPagerTabStrip/XL/Views/XLButtonBarView.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@

#import "XLPagerTabStripViewController.h"

typedef NS_ENUM(NSUInteger, XLPagerScroll) {
XLPagerScrollNO,
XLPagerScrollYES,
XLPagerScrollOnlyIfOutOfScreen
};

@interface XLButtonBarView : UICollectionView

Expand All @@ -35,9 +40,9 @@
@property UIFont * labelFont;
@property NSUInteger leftRightMargin;

-(void)moveToIndex:(NSUInteger)index animated:(BOOL)animated swipeDirection:(XLPagerTabStripDirection)swipeDirection;
-(void)moveToIndex:(NSUInteger)index animated:(BOOL)animated swipeDirection:(XLPagerTabStripDirection)swipeDirection pagerScroll:(XLPagerScroll)pagerScroll;

-(void)moveFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex withProgressPercentage:(CGFloat)progressPercentage;
-(void)moveFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex withProgressPercentage:(CGFloat)progressPercentage pagerScroll:(XLPagerScroll)pagerScroll;



Expand Down
42 changes: 23 additions & 19 deletions XLPagerTabStrip/XL/Views/XLButtonBarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ -(void)initializeXLButtonBarView
}


-(void)moveToIndex:(NSUInteger)index animated:(BOOL)animated swipeDirection:(XLPagerTabStripDirection)swipeDirection
-(void)moveToIndex:(NSUInteger)index animated:(BOOL)animated swipeDirection:(XLPagerTabStripDirection)swipeDirection pagerScroll:(XLPagerScroll)pagerScroll
{
self.selectedOptionIndex = index;
[self updateSelectedBarPositionWithAnimation:animated swipeDirection:swipeDirection];
[self updateSelectedBarPositionWithAnimation:animated swipeDirection:swipeDirection pagerScroll:pagerScroll];
}

-(void)moveFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex withProgressPercentage:(CGFloat)progressPercentage
-(void)moveFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex withProgressPercentage:(CGFloat)progressPercentage pagerScroll:(XLPagerScroll)pagerScroll
{
self.selectedOptionIndex = (progressPercentage > 0.5 ) ? toIndex : fromIndex;

Expand Down Expand Up @@ -110,21 +110,22 @@ -(void)moveFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex withProgres
if (self.contentSize.width > self.frame.size.width){
xValue = MIN(self.contentSize.width - self.frame.size.width, targetFrame.origin.x - offset <= 0 ? 0 : targetFrame.origin.x - offset);
}
//NSLog(@"X value: %@", @(xValue));
[self setContentOffset:CGPointMake(xValue, 0) animated:NO];
[self setContentOffset:CGPointMake(xValue, 0) animated:(ABS(self.contentOffset.x - xValue) > 30)];
self.selectedBar.frame = CGRectMake(targetFrame.origin.x, self.selectedBar.frame.origin.y, targetFrame.size.width, self.selectedBar.frame.size.height);
}


-(void)updateSelectedBarPositionWithAnimation:(BOOL)animation swipeDirection:(XLPagerTabStripDirection)swipeDirection
-(void)updateSelectedBarPositionWithAnimation:(BOOL)animation swipeDirection:(XLPagerTabStripDirection __unused)swipeDirection pagerScroll:(XLPagerScroll)pagerScroll
{
CGRect frame = self.selectedBar.frame;
UICollectionViewCell * cell = [self.dataSource collectionView:self cellForItemAtIndexPath:[NSIndexPath indexPathForItem:self.selectedOptionIndex inSection:0]];

[self updateContentOffset];
UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:self.selectedOptionIndex inSection:0]];
CGRect cellFrame = attributes.frame;

frame.size.width = cell.frame.size.width;
frame.origin.x = cell.frame.origin.x;
[self updateContentOffsetAnimated:animation pagerScroll:pagerScroll toFrame:cellFrame];

frame.size.width = cellFrame.size.width;
frame.origin.x = cellFrame.origin.x;
if (animation){
[UIView animateWithDuration:0.3 animations:^{
[self.selectedBar setFrame:frame];
Expand All @@ -139,19 +140,22 @@ -(void)updateSelectedBarPositionWithAnimation:(BOOL)animation swipeDirection:(XL

#pragma mark - Helpers

-(void)updateContentOffset
-(void)updateContentOffsetAnimated:(BOOL)animated pagerScroll:(XLPagerScroll)pageScroller toFrame:(CGRect)frame
{
UICollectionViewCell * cell = [self.dataSource collectionView:self cellForItemAtIndexPath:[NSIndexPath indexPathForItem:self.selectedOptionIndex inSection:0]];
if (cell){
NSUInteger offset = 35;
if (pageScroller != XLPagerScrollNO){
CGFloat leftInset = ((UICollectionViewFlowLayout *)self.collectionViewLayout).sectionInset.left;
if (pageScroller == XLPagerScrollOnlyIfOutOfScreen){
if (frame.origin.x >= self.contentOffset.x && frame.origin.x < (self.contentOffset.x + self.frame.size.width - leftInset)){
return;
}
}
NSUInteger offset = 30;
float xValue = MIN(
MAX(0,
self.contentSize.width - self.frame.size.width), // dont scroll if we are at the end of scroll view, if content is smaller than container width we scroll 0
MAX(((UICollectionViewFlowLayout *)self.collectionViewLayout).sectionInset.left - cell.frame.origin.x,
cell.frame.origin.x - ((UICollectionViewFlowLayout *)self.collectionViewLayout).sectionInset.left - offset)

self.collectionViewLayout.collectionViewContentSize.width - self.frame.size.width), // dont scroll if we are at the end of scroll view, if content is smaller than container width we scroll 0
MAX(leftInset - frame.origin.x, frame.origin.x - leftInset - offset)
);
[self setContentOffset:CGPointMake(xValue, 0) animated:YES];
[self setContentOffset:CGPointMake(xValue, 0) animated:animated];
}
}

Expand Down

0 comments on commit 033b656

Please sign in to comment.