From 52f7cdd89dd026b6bd4e085db9c17a54495460a7 Mon Sep 17 00:00:00 2001
From: Jake Schwartz <Jake@JakeSchwartz.com>
Date: Sun, 17 Jul 2016 20:03:30 -0700
Subject: [PATCH] Fixes a bug in which the pageAppeared delegate method is not
 called after tapping a page

This resolves [Issue #174: Delegate method pageAppeared doesn't being called]
---
 EAIntroView/EAIntroView.h |  3 +++
 EAIntroView/EAIntroView.m | 16 ++++++++++++++--
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/EAIntroView/EAIntroView.h b/EAIntroView/EAIntroView.h
index be32289..27a44b1 100644
--- a/EAIntroView/EAIntroView.h
+++ b/EAIntroView/EAIntroView.h
@@ -83,4 +83,7 @@ typedef NS_ENUM(NSUInteger, EAViewAlignment) {
 - (void)setCurrentPageIndex:(NSUInteger)currentPageIndex;
 - (void)setCurrentPageIndex:(NSUInteger)currentPageIndex animated:(BOOL)animated;
 
+
+- (void)scrollToPageForIndex:(NSUInteger)newPageIndex animated:(BOOL)animated;
+
 @end
diff --git a/EAIntroView/EAIntroView.m b/EAIntroView/EAIntroView.m
index 36be525..9010027 100644
--- a/EAIntroView/EAIntroView.m
+++ b/EAIntroView/EAIntroView.m
@@ -1007,7 +1007,17 @@ - (void)setCurrentPageIndex:(NSUInteger)currentPageIndex animated:(BOOL)animated
     
     _currentPageIndex = currentPageIndex;
     
-    CGFloat offset = currentPageIndex * self.scrollView.frame.size.width;
+    [self scrollToPageForIndex:currentPageIndex animated:animated];
+}
+
+- (void)scrollToPageForIndex:(NSUInteger)newPageIndex animated:(BOOL)animated
+{
+    if(![self pageForIndex:newPageIndex]) {
+        NSLog(@"Wrong newPageIndex received: %ld",(long)newPageIndex);
+        return;
+    }
+
+    CGFloat offset = newPageIndex * self.scrollView.frame.size.width;
     CGRect pageRect = { .origin.x = offset, .origin.y = 0.0, .size.width = self.scrollView.frame.size.width, .size.height = self.scrollView.frame.size.height };
     [self.scrollView scrollRectToVisible:pageRect animated:animated];
     
@@ -1023,7 +1033,9 @@ - (IBAction)goToNext:(id)sender {
     if(self.currentPageIndex + 1 >= [self.pages count]) {
         [self hideWithFadeOutDuration:0.3];
     } else {
-        [self setCurrentPageIndex:self.currentPageIndex + 1 animated:YES];
+        // Just scroll to the new page.
+        // After scrolling ends, we call -checkIndexForScrollView:, which itself sets the new currentPageIndex.
+        [self scrollToPageForIndex:self.currentPageIndex + 1 animated:YES];
     }
 }