Skip to content

Commit

Permalink
Merge pull request #18 from Nirvana-icy/fix-tap-during-scrolling-crash
Browse files Browse the repository at this point in the history
Fix the crash:Scroll the first page to second > when the scrolling do no...
  • Loading branch information
cwRichardKim committed Apr 28, 2015
2 parents 75422f8 + 50cb78b commit ff092a4
Showing 1 changed file with 56 additions and 25 deletions.
81 changes: 56 additions & 25 deletions RKSwipeBetweenViewControllers.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ @interface RKSwipeBetweenViewControllers ()

@property (nonatomic) UIScrollView *pageScrollView;
@property (nonatomic) NSInteger currentPageIndex;
/*
During Page Scrolling,we should avoild user to tap the segmentButton to fix the crash.
We find the crash situation:
Launch the demo > scrolling in the first page > and then quickly tap the fourth segment button => the app will crash
So I add isPageScrollingFlag to avoid user to tap the segment button during scrolling to fix the crash.
Thank you for RKSwipBetweenViewControllers.
**/
@property (nonatomic) BOOL isPageScrollingFlag;

@end

Expand Down Expand Up @@ -53,6 +65,7 @@ - (void)viewDidLoad
self.navigationBar.translucent = NO;
viewControllerArray = [[NSMutableArray alloc]init];
self.currentPageIndex = 0;
self.isPageScrollingFlag = NO;
}

#pragma mark Customizables
Expand Down Expand Up @@ -166,34 +179,38 @@ -(void)syncScrollView {
//so there's a loop that shows every view controller in the array up to the one you selected
//eg: if you're on page 1 and you click tab 3, then it shows you page 2 and then page 3
-(void)tapSegmentButtonAction:(UIButton *)button {
NSInteger tempIndex = self.currentPageIndex;

__weak typeof(self) weakSelf = self;

//%%% check to see if you're going left -> right or right -> left
if (button.tag > tempIndex) {
if (!self.isPageScrollingFlag) { // During Page Scrolling,we should avoild user to tap the segmentButton.

NSInteger tempIndex = self.currentPageIndex;

//%%% scroll through all the objects between the two points
for (int i = (int)tempIndex+1; i<=button.tag; i++) {
[pageController setViewControllers:@[[viewControllerArray objectAtIndex:i]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL complete){

//%%% if the action finishes scrolling (i.e. the user doesn't stop it in the middle),
//then it updates the page that it's currently on
if (complete) {
[weakSelf updateCurrentPageIndex:i];
}
}];
__weak typeof(self) weakSelf = self;

//%%% check to see if you're going left -> right or right -> left
if (button.tag > tempIndex) {

//%%% scroll through all the objects between the two points
for (int i = (int)tempIndex+1; i<=button.tag; i++) {
[pageController setViewControllers:@[[viewControllerArray objectAtIndex:i]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL complete){

//%%% if the action finishes scrolling (i.e. the user doesn't stop it in the middle),
//then it updates the page that it's currently on
if (complete) {
[weakSelf updateCurrentPageIndex:i];
}
}];
}
}
}

//%%% this is the same thing but for going right -> left
else if (button.tag < tempIndex) {
for (int i = (int)tempIndex-1; i >= button.tag; i--) {
[pageController setViewControllers:@[[viewControllerArray objectAtIndex:i]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:^(BOOL complete){
if (complete) {
[weakSelf updateCurrentPageIndex:i];
}
}];
//%%% this is the same thing but for going right -> left
else if (button.tag < tempIndex) {
for (int i = (int)tempIndex-1; i >= button.tag; i--) {
[pageController setViewControllers:@[[viewControllerArray objectAtIndex:i]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:^(BOOL complete){
if (complete) {
[weakSelf updateCurrentPageIndex:i];
}
}];
}
}
}
}
Expand Down Expand Up @@ -274,4 +291,18 @@ -(NSInteger)indexOfController:(UIViewController *)viewController {
return NSNotFound;
}

// =======================================================================
#pragma mark - ScrollViewDelegate 代理函数
// =======================================================================

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
self.isPageScrollingFlag = YES;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
self.isPageScrollingFlag = NO;
}

@end

0 comments on commit ff092a4

Please sign in to comment.