Skip to content
This repository has been archived by the owner on Apr 24, 2022. It is now read-only.

Update & Fix issue #45

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion ICViewPager.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Pod::Spec.new do |s|
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { "Ilter Cengiz" => "[email protected]" }
s.platform = :ios, '6.0'
s.source = { :git => "https://github.com/monsieurje/ICViewPager.git", :tag => "1.5.1" }
s.source = { :git => "https://github.com/monsieurje/ICViewPager.git", :tag => "1.5.3" }
s.source_files = 'ICViewPager/ICViewPager/*.{h,m}'
s.requires_arc = true

Expand Down
21 changes: 21 additions & 0 deletions ICViewPager/ICViewPager/ViewPagerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ typedef NS_ENUM(NSUInteger, ViewPagerOption) {
ViewPagerOptionTabOffset,
ViewPagerOptionTabWidth,
ViewPagerOptionTabLocation,
ViewPagerOptionTabNormalLineWidth,
ViewPagerOptionTabSelectedLineWidth,
ViewPagerOptionTabDisableTopLine,
ViewPagerOptionTabDisableBottomLine,
ViewPagerOptionStartFromSecondTab,
ViewPagerOptionCenterCurrentTab,
ViewPagerOptionFixFormerTabsPositions,
Expand Down Expand Up @@ -168,6 +172,23 @@ typedef NS_ENUM(NSUInteger, ViewPagerComponent) {
* @param index The index of the active tab
*/
- (void)viewPager:(ViewPagerController *)viewPager didChangeTabToIndex:(NSUInteger)index;
/**
* delegate object should implement this method if it wants to be informed when a tab changes and what its previous tab index was
*
* @param viewPager The viewPager that's subject to
* @param index The index of the active tab
* @param previousIndex The previous index of the active tab
*/
- (void)viewPager:(ViewPagerController *)viewPager didChangeTabToIndex:(NSUInteger)index fromIndex:(NSUInteger)previousIndex;
/**
* delegate object should implement this method if it wants to be informed when a tab changes and what its previous tab index was and whether the change action was caused by a swipe gesture or tab bar button press
*
* @param viewPager The viewPager that's subject to
* @param index The index of the active tab
* @param previousIndex The previous index of the active tab
* @param didSwipe Indicating if the change action was caused by a swipe gesture or a tab bar button press
*/
- (void)viewPager:(ViewPagerController *)viewPager didChangeTabToIndex:(NSUInteger)index fromIndex:(NSUInteger)previousIndex didSwipe:(BOOL)didSwipe;
/**
* Every time -reloadData method called, ViewPager will ask its delegate for option values.
* So you don't have to set options from ViewPager itself.
Expand Down
140 changes: 128 additions & 12 deletions ICViewPager/ICViewPager/ViewPagerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#define kTabOffset 56.0
#define kTabWidth 128.0
#define kTabLocation 1.0
#define kTabNormalLineWidth 1.0
#define kTabSelectedLineWidth 5.0
#define kTabDisableTopLine 0.0
#define kTabDisableBottomLine 0.0
#define kStartFromSecondTab 0.0
#define kCenterCurrentTab 0.0
#define kFixFormerTabsPositions 0.0
Expand Down Expand Up @@ -62,13 +66,19 @@ - (BOOL)isEqualToColor:(UIColor *)otherColor {
@interface TabView : UIView
@property (nonatomic, getter = isSelected) BOOL selected;
@property (nonatomic) UIColor *indicatorColor;
@property (nonatomic) BOOL disableTopLine;
@property (nonatomic) BOOL disableBottomLine;
@property (nonatomic) CGFloat normalLineWidth;
@property (nonatomic) CGFloat selectedLineWidth;
@end

@implementation TabView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.normalLineWidth = 1.0;
self.selectedLineWidth = 5.0;
}
return self;
}
Expand All @@ -86,15 +96,15 @@ - (void)drawRect:(CGRect)rect {
[bezierPath moveToPoint:CGPointMake(0.0, 0.0)];
[bezierPath addLineToPoint:CGPointMake(CGRectGetWidth(rect), 0.0)];
[[UIColor colorWithWhite:197.0/255.0 alpha:0.75] setStroke];
[bezierPath setLineWidth:1.0];
[bezierPath setLineWidth:(self.disableTopLine ? 0.0 : self.normalLineWidth)];
[bezierPath stroke];

// Draw bottom line
bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:CGPointMake(0.0, CGRectGetHeight(rect))];
[bezierPath addLineToPoint:CGPointMake(CGRectGetWidth(rect), CGRectGetHeight(rect))];
[[UIColor colorWithWhite:197.0/255.0 alpha:0.75] setStroke];
[bezierPath setLineWidth:1.0];
[bezierPath setLineWidth:(self.disableBottomLine ? 0.0 : self.normalLineWidth)];
[bezierPath stroke];

// Draw an indicator line if tab is selected
Expand All @@ -105,7 +115,7 @@ - (void)drawRect:(CGRect)rect {
// Draw the indicator
[bezierPath moveToPoint:CGPointMake(0.0, CGRectGetHeight(rect) - 1.0)];
[bezierPath addLineToPoint:CGPointMake(CGRectGetWidth(rect), CGRectGetHeight(rect) - 1.0)];
[bezierPath setLineWidth:5.0];
[bezierPath setLineWidth:self.selectedLineWidth];
[self.indicatorColor setStroke];
[bezierPath stroke];
}
Expand All @@ -131,6 +141,10 @@ @interface ViewPagerController () <UIPageViewControllerDataSource, UIPageViewCon
@property (nonatomic) NSNumber *tabOffset;
@property (nonatomic) NSNumber *tabWidth;
@property (nonatomic) NSNumber *tabLocation;
@property (nonatomic) NSNumber *tabNormalLineWidth;
@property (nonatomic) NSNumber *tabSelectedLineWidth;
@property (nonatomic) NSNumber *tabDisableTopLine;
@property (nonatomic) NSNumber *tabDisableBottomLine;
@property (nonatomic) NSNumber *startFromSecondTab;
@property (nonatomic) NSNumber *centerCurrentTab;
@property (nonatomic) NSNumber *fixFormerTabsPositions;
Expand All @@ -156,6 +170,10 @@ @implementation ViewPagerController
@synthesize tabOffset = _tabOffset;
@synthesize tabWidth = _tabWidth;
@synthesize tabLocation = _tabLocation;
@synthesize tabNormalLineWidth = _tabNormalLineWidth;
@synthesize tabSelectedLineWidth = _tabSelectedLineWidth;
@synthesize tabDisableTopLine = _tabDisableTopLine;
@synthesize tabDisableBottomLine = _tabDisableBottomLine;
@synthesize startFromSecondTab = _startFromSecondTab;
@synthesize centerCurrentTab = _centerCurrentTab;
@synthesize fixFormerTabsPositions = _fixFormerTabsPositions;
Expand Down Expand Up @@ -204,9 +222,9 @@ - (void)layoutSubviews {

CGFloat topLayoutGuide = 0.0;
if (IOS_VERSION_7) {
topLayoutGuide = 20.0;
if (self.navigationController && !self.navigationController.navigationBarHidden) {
topLayoutGuide += self.navigationController.navigationBar.frame.size.height;
if (self.navigationController.navigationBar.translucent) {
if (self.prefersStatusBarHidden == NO) topLayoutGuide += [UIApplication sharedApplication].statusBarFrame.size.height;
if (self.navigationController.navigationBarHidden == NO) topLayoutGuide += self.navigationController.navigationBar.bounds.size.height;
}
}

Expand All @@ -221,7 +239,7 @@ - (void)layoutSubviews {
frame.origin.x = 0.0;
frame.origin.y = [self.tabLocation boolValue] ? topLayoutGuide + CGRectGetHeight(self.tabsView.frame) : topLayoutGuide;
frame.size.width = CGRectGetWidth(self.view.frame);
frame.size.height = CGRectGetHeight(self.view.frame) - (topLayoutGuide + CGRectGetHeight(self.tabsView.frame)) - CGRectGetHeight(self.tabBarController.tabBar.frame);
frame.size.height = CGRectGetHeight(self.view.frame) - (topLayoutGuide + CGRectGetHeight(self.tabsView.frame)) - (self.tabBarController.tabBar.hidden ? 0 : CGRectGetHeight(self.tabBarController.tabBar.frame));
self.contentView.frame = frame;
}

Expand All @@ -236,7 +254,7 @@ - (IBAction)handleTapGesture:(id)sender {
//if Tap is not selected Tab(new Tab)
if (self.activeTabIndex != index) {
// Select the tab
[self selectTabAtIndex:index];
[self selectTabAtIndex:index didSwipe:NO];
}
}

Expand Down Expand Up @@ -285,6 +303,34 @@ - (void)setTabLocation:(NSNumber *)tabLocation {

_tabLocation = tabLocation;
}
- (void)setTabNormalLineWidth:(NSNumber *)tabNormalLineWidth {

if ([tabNormalLineWidth floatValue] < 0.0)
tabNormalLineWidth = [NSNumber numberWithFloat:0.0];

_tabNormalLineWidth = tabNormalLineWidth;
}
- (void)setTabSelectedLineWidth:(NSNumber *)tabSelectedLineWidth {

if ([tabSelectedLineWidth floatValue] < 0.0)
tabSelectedLineWidth = [NSNumber numberWithFloat:0.0];

_tabSelectedLineWidth = tabSelectedLineWidth;
}
- (void)setTabDisableTopLine:(NSNumber *)tabDisableTopLine {

if ([tabDisableTopLine floatValue] != 1.0 && [tabDisableTopLine floatValue] != 0.0)
tabDisableTopLine = [tabDisableTopLine boolValue] ? [NSNumber numberWithBool:YES] : [NSNumber numberWithBool:NO];

_tabDisableTopLine = tabDisableTopLine;
}
-(void)setTabDisableBottomLine:(NSNumber *)tabDisableBottomLine {

if ([tabDisableBottomLine floatValue] != 1.0 && [tabDisableBottomLine floatValue] != 0.0)
tabDisableBottomLine = [tabDisableBottomLine boolValue] ? [NSNumber numberWithBool:YES] : [NSNumber numberWithBool:NO];

_tabDisableBottomLine = tabDisableBottomLine;
}
- (void)setStartFromSecondTab:(NSNumber *)startFromSecondTab {

if ([startFromSecondTab floatValue] != 1.0 && [startFromSecondTab floatValue] != 0.0)
Expand Down Expand Up @@ -475,6 +521,46 @@ - (NSNumber *)tabLocation {
}
return _tabLocation;
}
- (NSNumber *)tabNormalLineWidth {

if (!_tabNormalLineWidth) {
CGFloat value = kTabNormalLineWidth;
if ([self.delegate respondsToSelector:@selector(viewPager:valueForOption:withDefault:)])
value = [self.delegate viewPager:self valueForOption:ViewPagerOptionTabNormalLineWidth withDefault:value];
self.tabNormalLineWidth = [NSNumber numberWithFloat:value];
}
return _tabNormalLineWidth;
}
- (NSNumber *)tabSelectedLineWidth {

if (!_tabSelectedLineWidth) {
CGFloat value = kTabSelectedLineWidth;
if ([self.delegate respondsToSelector:@selector(viewPager:valueForOption:withDefault:)])
value = [self.delegate viewPager:self valueForOption:ViewPagerOptionTabSelectedLineWidth withDefault:value];
self.tabSelectedLineWidth = [NSNumber numberWithFloat:value];
}
return _tabSelectedLineWidth;
}
- (NSNumber *)tabDisableTopLine {

if (!_tabDisableTopLine) {
CGFloat value = kTabDisableTopLine;
if ([self.delegate respondsToSelector:@selector(viewPager:valueForOption:withDefault:)])
value = [self.delegate viewPager:self valueForOption:ViewPagerOptionTabDisableTopLine withDefault:value];
self.tabDisableTopLine = [NSNumber numberWithFloat:value];
}
return _tabDisableTopLine;
}
- (NSNumber *)tabDisableBottomLine {

if (!_tabDisableBottomLine) {
CGFloat value = kTabDisableBottomLine;
if ([self.delegate respondsToSelector:@selector(viewPager:valueForOption:withDefault:)])
value = [self.delegate viewPager:self valueForOption:ViewPagerOptionTabDisableBottomLine withDefault:value];
self.tabDisableBottomLine = [NSNumber numberWithFloat:value];
}
return _tabDisableBottomLine;
}
- (NSNumber *)startFromSecondTab {

if (!_startFromSecondTab) {
Expand Down Expand Up @@ -560,6 +646,10 @@ - (void)reloadData {
_tabOffset = nil;
_tabWidth = nil;
_tabLocation = nil;
_tabNormalLineWidth = nil;
_tabSelectedLineWidth = nil;
_tabDisableTopLine = nil;
_tabDisableBottomLine = nil;
_startFromSecondTab = nil;
_centerCurrentTab = nil;
_fixFormerTabsPositions = nil;
Expand All @@ -573,14 +663,22 @@ - (void)reloadData {
// Call to setup again with the updated data
[self defaultSetup];
}

- (void)selectTabAtIndex:(NSUInteger)index {
[self selectTabAtIndex:index didSwipe:NO];
}

- (void)selectTabAtIndex:(NSUInteger)index didSwipe:(BOOL)didSwipe {

if (index >= self.tabCount) {
return;
}

self.animatingToTab = YES;

// Keep a reference to previousIndex in case it is needed for the delegate
NSUInteger previousIndex = self.activeTabIndex;

// Set activeTabIndex
self.activeTabIndex = index;

Expand All @@ -591,6 +689,12 @@ - (void)selectTabAtIndex:(NSUInteger)index {
if ([self.delegate respondsToSelector:@selector(viewPager:didChangeTabToIndex:)]) {
[self.delegate viewPager:self didChangeTabToIndex:self.activeTabIndex];
}
else if([self.delegate respondsToSelector:@selector(viewPager:didChangeTabToIndex:fromIndex:)]){
[self.delegate viewPager:self didChangeTabToIndex:self.activeTabIndex fromIndex:previousIndex];
}
else if ([self.delegate respondsToSelector:@selector(viewPager:didChangeTabToIndex:fromIndex:didSwipe:)]) {
[self.delegate viewPager:self didChangeTabToIndex:self.activeTabIndex fromIndex:previousIndex didSwipe:didSwipe];
}
}

- (void)setNeedsReloadOptions {
Expand Down Expand Up @@ -717,6 +821,14 @@ - (CGFloat)valueForOption:(ViewPagerOption)option {
return [[self tabWidth] floatValue];
case ViewPagerOptionTabLocation:
return [[self tabLocation] floatValue];
case ViewPagerOptionTabNormalLineWidth:
return [[self tabNormalLineWidth] floatValue];
case ViewPagerOptionTabSelectedLineWidth:
return [[self tabSelectedLineWidth] floatValue];
case ViewPagerOptionTabDisableTopLine:
return [[self tabDisableTopLine] floatValue];
case ViewPagerOptionTabDisableBottomLine:
return [[self tabDisableBottomLine] floatValue];
case ViewPagerOptionStartFromSecondTab:
return [[self startFromSecondTab] floatValue];
case ViewPagerOptionCenterCurrentTab:
Expand Down Expand Up @@ -747,7 +859,7 @@ - (void)defaultSettings {
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:nil];
[self addChildViewController:self.pageViewController];

// Setup some forwarding events to hijack the scrollView
// Keep a reference to the actual delegate
self.actualDelegate = ((UIScrollView *)[self.pageViewController.view.subviews objectAtIndex:0]).delegate;
Expand Down Expand Up @@ -862,7 +974,7 @@ - (void)defaultSetup {

// Select starting tab
NSUInteger index = [self.startFromSecondTab boolValue] ? 1 : 0;
[self selectTabAtIndex:index];
[self selectTabAtIndex:index didSwipe:NO];

// Set setup done
self.defaultSetupDone = YES;
Expand All @@ -875,7 +987,7 @@ - (TabView *)tabViewAtIndex:(NSUInteger)index {
}

if ([[self.tabs objectAtIndex:index] isEqual:[NSNull null]]) {

// Get view from dataSource
UIView *tabViewContent = [self.dataSource viewPager:self viewForTabAtIndex:index];
tabViewContent.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
Expand All @@ -885,6 +997,10 @@ - (TabView *)tabViewAtIndex:(NSUInteger)index {
[tabView addSubview:tabViewContent];
[tabView setClipsToBounds:YES];
[tabView setIndicatorColor:self.indicatorColor];
[tabView setNormalLineWidth:[self.tabNormalLineWidth floatValue]];
[tabView setSelectedLineWidth:[self.tabSelectedLineWidth floatValue]];
[tabView setDisableTopLine:[self.tabDisableTopLine boolValue]];
[tabView setDisableBottomLine:[self.tabDisableBottomLine boolValue]];

tabViewContent.center = tabView.center;

Expand Down Expand Up @@ -955,7 +1071,7 @@ - (void)pageViewController:(UIPageViewController *)pageViewController didFinishA

// Select tab
NSUInteger index = [self indexForViewController:viewController];
[self selectTabAtIndex:index];
[self selectTabAtIndex:index didSwipe:YES];
}

#pragma mark - UIScrollViewDelegate, Responding to Scrolling and Dragging
Expand Down
26 changes: 17 additions & 9 deletions ICViewPager/iPhone.storyboard
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="4510" systemVersion="12F37" targetRuntime="iOS.CocoaTouch" variant="6xAndEarlier" propertyAccessControl="none" useAutolayout="YES" initialViewController="KZy-Tn-xJf">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="4514" systemVersion="13B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="KZy-Tn-xJf">
<dependencies>
<deployment defaultVersion="1552" identifier="iOS"/>
<development version="4600" identifier="xcode"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3742"/>
<deployment defaultVersion="1536" identifier="iOS"/>
<development version="5000" identifier="xcode"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3747"/>
</dependencies>
<scenes>
<!--Host View Controller-->
<scene sceneID="g03-yH-dE6">
<objects>
<viewController id="whP-ZF-81a" customClass="HostViewController" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="uHQ-La-Efp"/>
<viewControllerLayoutGuide type="bottom" id="spg-Zp-2Vf"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="p79-qC-JP8">
<rect key="frame" x="0.0" y="64" width="320" height="504"/>
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
Expand All @@ -25,8 +29,12 @@
<scene sceneID="QBj-5a-OaV">
<objects>
<viewController storyboardIdentifier="contentViewController" id="eBC-7F-5HC" customClass="ContentViewController" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="TEN-2j-Zh9"/>
<viewControllerLayoutGuide type="bottom" id="3Lh-ul-rVq"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Rhb-jG-fQ8">
<rect key="frame" x="0.0" y="20" width="320" height="548"/>
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="dAg-1K-keU">
Expand All @@ -41,9 +49,9 @@
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="dAg-1K-keU" secondAttribute="trailing" constant="20" symbolic="YES" type="default" id="NKg-px-ryJ"/>
<constraint firstItem="dAg-1K-keU" firstAttribute="top" secondItem="Rhb-jG-fQ8" secondAttribute="top" constant="20" symbolic="YES" type="default" id="aTN-V5-W4B"/>
<constraint firstItem="dAg-1K-keU" firstAttribute="leading" secondItem="Rhb-jG-fQ8" secondAttribute="leading" constant="20" symbolic="YES" type="default" id="yRY-Du-d0N"/>
<constraint firstAttribute="trailing" secondItem="dAg-1K-keU" secondAttribute="trailing" constant="20" symbolic="YES" id="NKg-px-ryJ"/>
<constraint firstItem="dAg-1K-keU" firstAttribute="top" secondItem="Rhb-jG-fQ8" secondAttribute="top" constant="20" symbolic="YES" id="aTN-V5-W4B"/>
<constraint firstItem="dAg-1K-keU" firstAttribute="leading" secondItem="Rhb-jG-fQ8" secondAttribute="leading" constant="20" symbolic="YES" id="yRY-Du-d0N"/>
</constraints>
</view>
<connections>
Expand Down