Skip to content

Commit 87c1e1b

Browse files
committed
- exposed the page controller's internal scroll view
- remove the properties forwarding - exposed an option for checking whether the page controller's view is visible - fixed calling didNavigateToPageAtIndex: even if the page didn't change - fixed maximum content offset calculations
1 parent e8504e3 commit 87c1e1b

File tree

2 files changed

+36
-104
lines changed

2 files changed

+36
-104
lines changed

SCPageViewController/SCPageViewController.h

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
@import UIKit;
1010

11+
@class SCScrollView;
12+
1113
@protocol SCPageViewControllerDataSource;
1214
@protocol SCPageViewControllerDelegate;
1315
@protocol SCPageLayouterProtocol;
@@ -127,19 +129,22 @@
127129
@property (nonatomic, weak) id<SCPageViewControllerDelegate> delegate;
128130

129131

132+
/** The internal scroll view */
133+
@property (nonatomic, strong, readonly) SCScrollView *scrollView;
134+
130135

131136
/** The current page in the page view controller */
132137
@property (nonatomic, readonly) NSUInteger currentPage;
133138

134139

135-
/** The current content offset in the page view controller's scroll view */
136-
@property (nonatomic, readonly) CGPoint contentOffset;
137-
138-
139140
/** The total number of pages the page view controllers holds at any given time */
140141
@property (nonatomic, readonly) NSUInteger numberOfPages;
141142

142143

144+
/** Whether the page controller's view is visible or not */
145+
@property (nonatomic, readonly) BOOL visible;
146+
147+
143148
/** An array of currently loaded view controllers in the page view controller */
144149
@property (nonatomic, readonly) NSArray *loadedViewControllers;
145150

@@ -148,35 +153,6 @@
148153
@property (nonatomic, readonly) NSArray *visibleViewControllers;
149154

150155

151-
152-
/** UIBezierPath inside which the pageController's scrollView doesn't respond to touch events */
153-
@property (nonatomic, strong) UIBezierPath *touchRefusalArea;
154-
155-
156-
/** Boolean value that controls whether the pageController's scrollView bounces past the
157-
* edge of content and back again
158-
*
159-
* Default value is set to true
160-
*/
161-
@property (nonatomic, assign) BOOL bounces;
162-
163-
164-
/** A Boolean value that determines whether scrolling is enabled for the pageController's
165-
* scrollView.
166-
*
167-
* Default value is set to true
168-
*/
169-
@property (nonatomic, assign) BOOL scrollEnabled;
170-
171-
172-
/** A Boolean value that controls whether the pageController's scrollView indicators are
173-
* visible.
174-
*
175-
* Default value is set to false
176-
*/
177-
@property (nonatomic, assign) BOOL showsScrollIndicators;
178-
179-
180156
/** A Boolean value that determines whether paging is enabled for the pageController's
181157
* scrollView.
182158
*
@@ -203,20 +179,6 @@
203179
@property (nonatomic, assign) BOOL shouldLayoutPagesOnRest;
204180

205181

206-
/** The minimum number of fingers that can be touching the view for this gesture to be recognized.
207-
*
208-
* Default value is set to 1
209-
*/
210-
@property (nonatomic, assign) NSUInteger minimumNumberOfTouches;
211-
212-
213-
/** The maximum number of fingers that can be touching the view for this gesture to be recognized.
214-
*
215-
* Default value is set to NSUIntegerMax
216-
*/
217-
@property (nonatomic, assign) NSUInteger maximumNumberOfTouches;
218-
219-
220182
/** Timing function used when navigating beteen pages
221183
*
222184
* Default value is set to SCEasingFunctionTypeSineEaseInOut
@@ -231,13 +193,6 @@
231193
@property (nonatomic, assign) NSTimeInterval animationDuration;
232194

233195

234-
/** The pageViewController's scroll view deceleration rate
235-
*
236-
* Defaults to UIScrollViewDecelerationRateFast
237-
*/
238-
@property (nonatomic, assign) CGFloat decelerationRate;
239-
240-
241196
@end
242197

243198

SCPageViewController/SCPageViewController.m

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,6 @@ @interface SCPageViewController () <SCPageViewControllerViewDelegate, UIScrollVi
6060
@end
6161

6262
@implementation SCPageViewController
63-
@dynamic contentOffset;
64-
@dynamic bounces;
65-
@dynamic touchRefusalArea;
66-
@dynamic showsScrollIndicators;
67-
@dynamic minimumNumberOfTouches;
68-
@dynamic maximumNumberOfTouches;
69-
@dynamic scrollEnabled;
70-
@dynamic decelerationRate;
7163

7264
- (void)dealloc
7365
{
@@ -104,6 +96,14 @@ - (void)_commonSetup
10496

10597
self.layouterContentInset = UIEdgeInsetsZero;
10698
self.layouterInterItemSpacing = 0.0f;
99+
100+
self.scrollView = [[SCScrollView alloc] init];
101+
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
102+
self.scrollView.showsVerticalScrollIndicator = NO;
103+
self.scrollView.showsHorizontalScrollIndicator = NO;
104+
self.scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
105+
self.scrollView.delegate = self;
106+
self.scrollView.clipsToBounds = NO;
107107
}
108108

109109
- (void)loadView
@@ -118,14 +118,7 @@ - (void)viewDidLoad
118118

119119
[self.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
120120

121-
self.scrollView = [[SCScrollView alloc] initWithFrame:self.view.bounds];
122-
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
123-
self.scrollView.showsVerticalScrollIndicator = NO;
124-
self.scrollView.showsHorizontalScrollIndicator = NO;
125-
self.scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
126-
self.scrollView.delegate = self;
127-
self.scrollView.clipsToBounds = NO;
128-
121+
[self.scrollView setFrame:self.view.bounds];
129122
[self.view addSubview:self.scrollView];
130123

131124
[self reloadData];
@@ -167,12 +160,14 @@ - (void)setLayouter:(id<SCPageLayouterProtocol>)layouter
167160
}
168161
}];
169162

170-
if(animated) {
171-
[UIView animateWithDuration:self.animationDuration delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction animations:^{
172-
[self navigateToPageAtIndex:pageIndex animated:NO completion:nil];
173-
} completion:nil];
174-
} else {
175-
[self navigateToPageAtIndex:pageIndex animated:animated completion:nil];
163+
if(!self.scrollView.isRunningAnimation) {
164+
if(animated) {
165+
[UIView animateWithDuration:self.animationDuration delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction animations:^{
166+
[self navigateToPageAtIndex:pageIndex animated:NO completion:nil];
167+
} completion:nil];
168+
} else {
169+
[self navigateToPageAtIndex:pageIndex animated:animated completion:nil];
170+
}
176171
}
177172
}
178173

@@ -243,6 +238,8 @@ - (void)navigateToPageAtIndex:(NSUInteger)pageIndex
243238
animated:(BOOL)animated
244239
completion:(void(^)())completion
245240
{
241+
NSUInteger previousCurrentPage = self.currentPage;
242+
246243
if(pageIndex >= self.numberOfPages) {
247244
return;
248245
}
@@ -264,7 +261,7 @@ - (void)navigateToPageAtIndex:(NSUInteger)pageIndex
264261

265262
[self _updateNavigationContraints];
266263

267-
if(!animated && [self.delegate respondsToSelector:@selector(pageViewController:didNavigateToPageAtIndex:)]) {
264+
if(!animated && previousCurrentPage != self.currentPage && [self.delegate respondsToSelector:@selector(pageViewController:didNavigateToPageAtIndex:)]) {
268265
[self.delegate pageViewController:self didNavigateToPageAtIndex:pageIndex];
269266
}
270267

@@ -342,6 +339,11 @@ - (NSUInteger)pageIndexForViewController:(UIViewController *)viewController
342339
return pageIndex;
343340
}
344341

342+
- (BOOL)visible
343+
{
344+
return self.isViewVisible;
345+
}
346+
345347
#pragma mark - Navigational Constraints
346348

347349
- (void)_updateBoundsAndConstraints
@@ -626,31 +628,6 @@ - (BOOL)shouldAutomaticallyForwardAppearanceMethods
626628
return NO;
627629
}
628630

629-
#pragma mark - Properties and forwarding
630-
631-
- (BOOL)showsScrollIndicators
632-
{
633-
return [self.scrollView showsHorizontalScrollIndicator] && [self.scrollView showsVerticalScrollIndicator];
634-
}
635-
636-
- (void)setShowsScrollIndicators:(BOOL)showsScrollIndicators
637-
{
638-
[self.scrollView setShowsHorizontalScrollIndicator:showsScrollIndicators];
639-
[self.scrollView setShowsVerticalScrollIndicator:showsScrollIndicators];
640-
}
641-
642-
- (id)forwardingTargetForSelector:(SEL)aSelector
643-
{
644-
if([self.scrollView respondsToSelector:aSelector]) {
645-
return self.scrollView;
646-
} else if([self.scrollView.panGestureRecognizer respondsToSelector:aSelector]) {
647-
return self.scrollView.panGestureRecognizer;
648-
} else {
649-
[NSException raise:@"SCPageViewControllerUnrecognizedSelectorException" format:@"Unrecognized selector %@", NSStringFromSelector(aSelector)];
650-
return nil;
651-
}
652-
}
653-
654631
#pragma mark - UIScrollViewDelegate
655632

656633
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
@@ -819,10 +796,10 @@ - (CGPoint)_nextStepOffsetForFrame:(CGRect)finalFrame withVelocity:(CGPoint)velo
819796
} else if(velocity.x > 0.0f) {
820797
nextStepOffset.x = (NSInteger)CGRectGetMaxX(finalFrame);
821798
} else if(velocity.y < 0.0f) {
822-
CGFloat maxOffset = self.scrollView.contentSize.height - CGRectGetHeight(self.scrollView.bounds) + self.layouterContentInset.top + self.layouterContentInset.bottom;
799+
CGFloat maxOffset = MAX(self.scrollView.contentSize.height, CGRectGetHeight(self.scrollView.bounds)) - CGRectGetHeight(self.scrollView.bounds) + self.layouterContentInset.top + self.layouterContentInset.bottom;
823800
nextStepOffset.y = MIN(maxOffset, (NSInteger)CGRectGetMinY(finalFrame));
824801
} else if(velocity.x < 0.0f) {
825-
CGFloat maxOffset = self.scrollView.contentSize.width - CGRectGetWidth(self.scrollView.bounds) + self.layouterContentInset.left + self.layouterContentInset.right;
802+
CGFloat maxOffset = MAX(self.scrollView.contentSize.width, CGRectGetWidth(self.scrollView.bounds)) - CGRectGetWidth(self.scrollView.bounds) + self.layouterContentInset.left + self.layouterContentInset.right;
826803
nextStepOffset.x = MIN(maxOffset, (NSInteger)CGRectGetMinX(finalFrame));
827804
}
828805

0 commit comments

Comments
 (0)