From 98b24a01f8520f32302efac962f41dc98a2e612b Mon Sep 17 00:00:00 2001 From: Damjan Dimovski Date: Fri, 24 Feb 2017 11:09:45 +0100 Subject: [PATCH] Fixed #201 Fixes an issue where current item index was being changed on device rotation. --- SwipeView/SwipeView.m | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/SwipeView/SwipeView.m b/SwipeView/SwipeView.m index 04e942b..827f8f7 100755 --- a/SwipeView/SwipeView.m +++ b/SwipeView/SwipeView.m @@ -480,6 +480,14 @@ - (void)updateScrollViewDimensions if (!CGSizeEqualToSize(_scrollView.contentSize, contentSize)) { + // contentSize is changed, make sure the content offset is adjusted based on the current item index + CGPoint newContentOffset; + if (_vertical) { + newContentOffset = CGPointMake(0.f, _currentItemIndex * _itemSize.height); + } else { + newContentOffset = CGPointMake(_currentItemIndex * _itemSize.width, 0.f); + } + [self setContentOffsetWithoutEvent:newContentOffset]; _scrollView.contentSize = contentSize; } } @@ -724,11 +732,14 @@ - (void)setContentOffsetWithoutEvent:(CGPoint)contentOffset { if (!CGPointEqualToPoint(_scrollView.contentOffset, contentOffset)) { + BOOL old = _suppressScrollEvent; + BOOL animationEnabled = [UIView areAnimationsEnabled]; if (animationEnabled) [UIView setAnimationsEnabled:NO]; _suppressScrollEvent = YES; _scrollView.contentOffset = contentOffset; _suppressScrollEvent = NO; + _suppressScrollEvent = old; if (animationEnabled) [UIView setAnimationsEnabled:YES]; } } @@ -1000,7 +1011,7 @@ - (void)reloadData //get number of items [self updateItemSizeAndCount]; - + //layout views [self setNeedsLayout]; @@ -1158,6 +1169,7 @@ - (void)scrollViewDidScroll:(__unused UIScrollView *)scrollView - (void)scrollViewWillBeginDragging:(__unused UIScrollView *)scrollView { + _suppressScrollEvent = NO; [_delegate swipeViewWillBeginDragging:self]; //force refresh @@ -1195,6 +1207,7 @@ - (void)scrollViewDidEndDecelerating:(__unused UIScrollView *)scrollView [self didScroll]; [_delegate swipeViewDidEndDecelerating:self]; + _suppressScrollEvent = YES; } @end