Permalink
Browse files

Disable user interaction on pages that are not the current one.

  • Loading branch information...
1 parent f7115db commit b411efd13311dc1a9f4b1e88a72e05de0d854a36 igerm committed Apr 26, 2012
Showing with 34 additions and 4 deletions.
  1. +34 −4 INPageScrollView/Clases/INPagedScrollView.m
@@ -58,12 +58,17 @@ - (id) initWithCoder:(NSCoder *)aDecoder {
return self;
}
--(void)setContentOffset:(CGPoint)newOffset {
+
+- (NSInteger)pageIndexFromOffsetPoint:(CGPoint)offset {
+ CGFloat pageWidth = self.frame.size.width;
+ return floor((offset.x - pageWidth / 2) / pageWidth) + 1;
+}
+
+- (void)setContentOffset:(CGPoint)newOffset {
if (self.contentOffset.x != newOffset.x && !haltScrollingTriggeredEvents) {
- CGFloat pageWidth = self.frame.size.width;
- currentPage = floor((newOffset.x - pageWidth / 2) / pageWidth) + 1;
+ currentPage = [self pageIndexFromOffsetPoint:newOffset];
[self unloadPage:currentPage-2];
[self unloadPage:currentPage+2];
@@ -73,9 +78,24 @@ -(void)setContentOffset:(CGPoint)newOffset {
}
+ //don't always set current page because it will mess the inserting/deleting of pages
+ int _currentPage = [self pageIndexFromOffsetPoint:newOffset];
+
+ [self setUserInteractionEnabled:NO forPage:_currentPage-1];
+ [self setUserInteractionEnabled:YES forPage:_currentPage];
+ [self setUserInteractionEnabled:NO forPage:_currentPage+1];
+
[super setContentOffset:newOffset];
}
+- (void) setUserInteractionEnabled:(BOOL)userInteractionEnabled forPage:(NSInteger)page {
+
+ if (pageCount <=0 || page>pageCount-1 || page <0) return;
+
+ INPageView * pageView = [self.loadedPages objectForKey:[NSNumber numberWithInteger:page]];
+ pageView.userInteractionEnabled = userInteractionEnabled;
+}
+
- (void) setPagedScrollDelegate:(id<INPagedScrollViewDelegate>)aDelegate {
if (pagedScrollDelegate != aDelegate) {
pagedScrollDelegate = aDelegate;
@@ -294,14 +314,20 @@ -(void) insertPageAtIndex:(NSInteger)indexBeingInserted {
} completion:^(BOOL finished) {
+ //update current page in case something else changed it (like insert and then scrolling while animation is in place)
+ currentPage = [self pageIndexFromOffsetPoint:self.contentOffset];
+
haltScrollingTriggeredEvents = NO;
+
[self unloadPage:currentPage-2];
[self unloadPage:currentPage+2];
+
}];
}
else {
self.contentOffset = CGPointMake(self.frame.size.width*currentPage, 0);
+
for (INPageView * pageView in self.subviews) {
if ([pageView isKindOfClass:[INPageView class]]) {
@@ -405,6 +431,9 @@ -(void) removePageAtIndex:(NSInteger)indexBeingRemoved {
} completion:^(BOOL finished) {
+ //update current page in case something else changed it (like insert and then scrolling while animation is in place)
+ currentPage = [self pageIndexFromOffsetPoint:self.contentOffset];
+
[self reloadContentSize];
[self.unusedPages addObject:pageViewBeingRemoved];
pageViewBeingRemoved.alpha = 1;
@@ -423,6 +452,7 @@ -(void) removePageAtIndex:(NSInteger)indexBeingRemoved {
self.contentOffset = CGPointMake(self.frame.size.width*currentPage, 0);
+
[self.unusedPages addObject:pageViewBeingRemoved];
[pageViewBeingRemoved removeFromSuperview];
@@ -487,7 +517,7 @@ -(void) scrollToPageIndex:(NSInteger)page animated:(BOOL)animated{
}
[self setContentOffset:CGPointMake(self.frame.size.width*page, 0) animated:animated];
- //self.contentOffset = CGPointMake(self.frame.size.width*page, 0);
+
}
- (NSInteger)indexForCurrentPage {
return currentPage;

0 comments on commit b411efd

Please sign in to comment.