Permalink
Browse files

[paging-scroll-view] Fix an exception thrown when NIPagingScrollView …

…rotates with a zero frame 💥

When -pageScrollableDimension returns 0, _percentScrolledIntoFirstVisiblePage ends up as NaN because of divide-by-zero operations in willRotateToInterfaceOrientation:duration:. This patch guards against these divide-by-zero errors.

Also add tests to catch exceptions thrown when rotating NIPagingScrollView with a zero frame.
  • Loading branch information...
1 parent fc0aa3f commit 68252d53d0ad8ccef5cc3e297553b8dc0cad8156 @stephanemoore stephanemoore committed Sep 15, 2017
@@ -645,6 +645,12 @@ - (void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOri
CGFloat offset = [self scrolledPageOffset];
CGFloat pageScrollableDimension = [self pageScrollableDimension];
+ if (pageScrollableDimension == 0) {
+ _firstVisiblePageIndexBeforeRotation = 0;
+ _percentScrolledIntoFirstVisiblePage = 0.f;
+ return;
+ }
+
if (offset >= 0) {
_firstVisiblePageIndexBeforeRotation = (NSInteger)NICGFloatFloor(offset / pageScrollableDimension);
_percentScrolledIntoFirstVisiblePage = ((offset
@@ -23,11 +23,17 @@
@interface NIPagingScrollViewTests : XCTestCase
@end
-
@implementation NIPagingScrollViewTests
+/** Test that rotating with a zero frame does not throw exceptions. */
+- (void)testRotationWithZeroFrame {
+ NIPagingScrollView *pagingScrollView = [[NIPagingScrollView alloc] initWithFrame:CGRectZero];
+ UIInterfaceOrientation targetInterfaceOrientation = UIInterfaceOrientationPortrait;
+ XCTAssertNoThrow([pagingScrollView willRotateToInterfaceOrientation:targetInterfaceOrientation
+ duration:0.25]);
+ XCTAssertNoThrow([pagingScrollView willAnimateRotationToInterfaceOrientation:targetInterfaceOrientation
+ duration:0.25]);
-- (void)testNothing {
}
@end

0 comments on commit 68252d5

Please sign in to comment.