From 5c85915b5829d16129ee00f3eaba8ad0af2be552 Mon Sep 17 00:00:00 2001
From: Matt Holgate <matt@emobix.co.uk>
Date: Wed, 16 Jul 2014 18:33:25 +0100
Subject: Fix page changing when rotating the device.

A bug in UIKit means our 'scrollViewDidScroll' method is called during
screen rotation. This ended up corrupting our current page number because
the width of the screen had changed at this point, but the pages hadn't
yet been resized/repositioned (I assume).

The workaround is to ignore calls to scrollViewDidScroll during rotation.
---
 platform/ios/Classes/MuDocumentController.m | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

(limited to 'platform/ios/Classes')

diff --git a/platform/ios/Classes/MuDocumentController.m b/platform/ios/Classes/MuDocumentController.m
index 36f2c867..35982ba7 100644
--- a/platform/ios/Classes/MuDocumentController.m
+++ b/platform/ios/Classes/MuDocumentController.m
@@ -123,6 +123,9 @@ static void saveDoc(char *current_path, fz_document *doc)
 }
 
 @implementation MuDocumentController
+{
+	BOOL _isRotating;
+}
 
 - (id) initWithFilename: (NSString*)filename path:(char *)cstr document: (MuDocRef *)aDoc
 {
@@ -974,6 +977,16 @@ static void saveDoc(char *current_path, fz_document *doc)
 
 - (void) scrollViewDidScroll: (UIScrollView*)scrollview
 {
+	// scrollViewDidScroll seems to get called part way through a screen rotation.
+	// (This is possibly a UIScrollView bug - see
+	// http://stackoverflow.com/questions/4123991/uiscrollview-disable-scrolling-while-rotating-on-iphone-ipad/8141423#8141423 ).
+	// This ends up corrupting the current page number, because the calculation
+	// 'current = x / width' is using the new value of 'width' before the
+	// pages have been resized/repositioned. To avoid this problem, we filter out
+	// calls to scrollViewDidScroll during rotation.
+	if (_isRotating)
+		return;
+
 	if (width == 0)
 		return; // not visible yet
 
@@ -1097,8 +1110,18 @@ static void saveDoc(char *current_path, fz_document *doc)
 	return YES;
 }
 
+- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
+{
+	_isRotating = YES;
+}
+
 - (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation)o
 {
+	_isRotating = NO;
+
+	// We need to set these here, because during the animation we may use a wider
+	// size (the maximum of the landscape/portrait widths), to avoid clipping during
+	// the rotation.
 	[canvas setContentSize: CGSizeMake(fz_count_pages(doc) * width, height)];
 	[canvas setContentOffset: CGPointMake(current * width, 0)];
 }
-- 
cgit v1.2.3