From cee53a872a217dec109cc46ac14e7f843d8b905f Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Wed, 19 Oct 2016 14:35:15 +0900 Subject: [PATCH 01/10] Change event call point from scrollViewDidScroll to scrollViewDidEndDecelerating for swipeViewCurrentItemIndexDidChange Now, It can call correct index chaning event for swipe whether user cancel swipe gesture or not. --- SwipeView/SwipeView.m | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/SwipeView/SwipeView.m b/SwipeView/SwipeView.m index 04e942b..cf886cb 100755 --- a/SwipeView/SwipeView.m +++ b/SwipeView/SwipeView.m @@ -273,7 +273,7 @@ - (void)setDecelerationRate:(float)decelerationRate - (void)setAutoscroll:(CGFloat)autoscroll { - if (fabs(_autoscroll - autoscroll) > 0.0001f) + if (fabsf(_autoscroll - autoscroll) > 0.0001f) { _autoscroll = autoscroll; if (autoscroll) [self startAnimation]; @@ -391,11 +391,11 @@ - (void)updateScrollOffset _scrollOffset = [self clampedOffset:_scrollOffset]; } } - if (_vertical && fabs(_scrollView.contentOffset.x) > 0.0001f) + if (_vertical && fabsf(_scrollView.contentOffset.x) > 0.0001f) { [self setContentOffsetWithoutEvent:CGPointMake(0.0f, _scrollView.contentOffset.y)]; } - else if (!_vertical && fabs(_scrollView.contentOffset.y) > 0.0001f) + else if (!_vertical && fabsf(_scrollView.contentOffset.y) > 0.0001f) { [self setContentOffsetWithoutEvent:CGPointMake(_scrollView.contentOffset.x, 0.0f)]; } @@ -613,7 +613,7 @@ - (void)didScroll [self layOutItemViews]; [_delegate swipeViewDidScroll:self]; - if (!_defersItemViewLoading || fabs([self minScrollDistanceFromOffset:_lastUpdateOffset toOffset:_scrollOffset]) >= 1.0f) + if (!_defersItemViewLoading || fabsf([self minScrollDistanceFromOffset:_lastUpdateOffset toOffset:_scrollOffset]) >= 1.0f) { //update item index _currentItemIndex = [self clampedIndex:roundf(_scrollOffset)]; @@ -621,13 +621,6 @@ - (void)didScroll //load views _lastUpdateOffset = _currentItemIndex; [self loadUnloadViews]; - - //send index update event - if (_previousItemIndex != _currentItemIndex) - { - _previousItemIndex = _currentItemIndex; - [_delegate swipeViewCurrentItemIndexDidChange:self]; - } } } @@ -773,7 +766,7 @@ - (CGFloat)minScrollDistanceFromOffset:(CGFloat)fromOffset toOffset:(CGFloat)toO { wrappedDistance = -wrappedDistance; } - return (fabs(directDistance) <= fabs(wrappedDistance))? directDistance: wrappedDistance; + return (fabsf(directDistance) <= fabsf(wrappedDistance))? directDistance: wrappedDistance; } return directDistance; } @@ -794,7 +787,7 @@ - (void)setCurrentPage:(NSInteger)currentPage - (void)setScrollOffset:(CGFloat)scrollOffset { - if (fabs(_scrollOffset - scrollOffset) > 0.0001f) + if (fabsf(_scrollOffset - scrollOffset) > 0.0001f) { _scrollOffset = scrollOffset; _lastUpdateOffset = _scrollOffset - 1.0f; //force refresh @@ -1185,7 +1178,7 @@ - (void)scrollViewDidEndDecelerating:(__unused UIScrollView *)scrollView { //prevent rounding errors from accumulating CGFloat integerOffset = roundf(_scrollOffset); - if (fabs(_scrollOffset - integerOffset) < 0.01f) + if (fabsf(_scrollOffset - integerOffset) < 0.01f) { _scrollOffset = integerOffset; } @@ -1195,6 +1188,13 @@ - (void)scrollViewDidEndDecelerating:(__unused UIScrollView *)scrollView [self didScroll]; [_delegate swipeViewDidEndDecelerating:self]; + + //send index update event + if (_previousItemIndex != _currentItemIndex) + { + _previousItemIndex = _currentItemIndex; + [_delegate swipeViewCurrentItemIndexDidChange:self]; + } } @end From 479f3c23f2ba620f4d4e6e1b079227cbbac06eea Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Tue, 25 Oct 2016 13:50:14 +0900 Subject: [PATCH 02/10] Update Podspec add deployment_target 7.0 --- SwipeView.podspec | 1 + 1 file changed, 1 insertion(+) diff --git a/SwipeView.podspec b/SwipeView.podspec index de5b8eb..7e58c67 100644 --- a/SwipeView.podspec +++ b/SwipeView.podspec @@ -10,4 +10,5 @@ Pod::Spec.new do |s| s.source_files = 'SwipeView' s.requires_arc = true s.platform = :ios + s.ios.deployment_target = '7.0' end \ No newline at end of file From b3dfe904ecc770603535aa140cba7c10146a2519 Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Tue, 25 Oct 2016 14:18:55 +0900 Subject: [PATCH 03/10] property set to weak dataSource, delegate --- SwipeView/SwipeView.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SwipeView/SwipeView.h b/SwipeView/SwipeView.h index 2fb12dd..89c5772 100755 --- a/SwipeView/SwipeView.h +++ b/SwipeView/SwipeView.h @@ -59,8 +59,8 @@ typedef NS_ENUM(NSUInteger, SwipeViewAlignment) @interface SwipeView : UIView -@property (nonatomic, weak_delegate) IBOutlet id dataSource; -@property (nonatomic, weak_delegate) IBOutlet id delegate; +@property (nonatomic, weak) IBOutlet id dataSource; +@property (nonatomic, weak) IBOutlet id delegate; @property (nonatomic, readonly) NSInteger numberOfItems; @property (nonatomic, readonly) NSInteger numberOfPages; @property (nonatomic, readonly) CGSize itemSize; From 586a325f3f5a13d7947c9ad058b6416302c2474f Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Sun, 4 Dec 2016 01:15:03 +0900 Subject: [PATCH 04/10] Add contentInset --- SwipeView/SwipeView.h | 1 + SwipeView/SwipeView.m | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/SwipeView/SwipeView.h b/SwipeView/SwipeView.h index 89c5772..f6282ab 100755 --- a/SwipeView/SwipeView.h +++ b/SwipeView/SwipeView.h @@ -85,6 +85,7 @@ typedef NS_ENUM(NSUInteger, SwipeViewAlignment) @property (nonatomic, readonly, getter = isScrolling) BOOL scrolling; @property (nonatomic, assign) BOOL defersItemViewLoading; @property (nonatomic, assign, getter = isVertical) BOOL vertical; +@property (nonatomic, assign) UIEdgeInsets contentInset; - (void)reloadData; - (void)reloadItemAtIndex:(NSInteger)index; diff --git a/SwipeView/SwipeView.m b/SwipeView/SwipeView.m index cf886cb..e25f7b0 100755 --- a/SwipeView/SwipeView.m +++ b/SwipeView/SwipeView.m @@ -165,6 +165,13 @@ - (void)dealloc [_timer invalidate]; } +- (void)setContentInset:(UIEdgeInsets)contentInset { + if (!UIEdgeInsetsEqualToEdgeInsets(contentInset, _contentInset)) { + _contentInset = contentInset; + _scrollView.contentInset = contentInset; + } +} + - (void)setDataSource:(id)dataSource { if (_dataSource != dataSource) From f822d996b23e180ed2a4cd54f2dfedaedb027ce4 Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Tue, 6 Dec 2016 18:44:33 +0900 Subject: [PATCH 05/10] Remove property contentInset and add scrollView --- SwipeView/SwipeView.h | 2 +- SwipeView/SwipeView.m | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/SwipeView/SwipeView.h b/SwipeView/SwipeView.h index f6282ab..5cd72b8 100755 --- a/SwipeView/SwipeView.h +++ b/SwipeView/SwipeView.h @@ -85,7 +85,7 @@ typedef NS_ENUM(NSUInteger, SwipeViewAlignment) @property (nonatomic, readonly, getter = isScrolling) BOOL scrolling; @property (nonatomic, assign) BOOL defersItemViewLoading; @property (nonatomic, assign, getter = isVertical) BOOL vertical; -@property (nonatomic, assign) UIEdgeInsets contentInset; +@property (nonatomic, readonly) UIScrollView *scrollView; - (void)reloadData; - (void)reloadItemAtIndex:(NSInteger)index; diff --git a/SwipeView/SwipeView.m b/SwipeView/SwipeView.m index e25f7b0..cf886cb 100755 --- a/SwipeView/SwipeView.m +++ b/SwipeView/SwipeView.m @@ -165,13 +165,6 @@ - (void)dealloc [_timer invalidate]; } -- (void)setContentInset:(UIEdgeInsets)contentInset { - if (!UIEdgeInsetsEqualToEdgeInsets(contentInset, _contentInset)) { - _contentInset = contentInset; - _scrollView.contentInset = contentInset; - } -} - - (void)setDataSource:(id)dataSource { if (_dataSource != dataSource) From 5917aea7bf8b4f99a3cf25efca21d26be62adc28 Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Tue, 13 Dec 2016 16:59:53 +0900 Subject: [PATCH 06/10] Swipe bug fix with previousItemIndex --- SwipeView/SwipeView.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SwipeView/SwipeView.m b/SwipeView/SwipeView.m index cf886cb..f6f2fa1 100755 --- a/SwipeView/SwipeView.m +++ b/SwipeView/SwipeView.m @@ -773,6 +773,7 @@ - (CGFloat)minScrollDistanceFromOffset:(CGFloat)fromOffset toOffset:(CGFloat)toO - (void)setCurrentItemIndex:(NSInteger)currentItemIndex { + _previousItemIndex = _currentItemIndex; _currentItemIndex = currentItemIndex; self.scrollOffset = currentItemIndex; } @@ -1155,6 +1156,7 @@ - (void)scrollViewWillBeginDragging:(__unused UIScrollView *)scrollView //force refresh _lastUpdateOffset = self.scrollOffset - 1.0f; + _previousItemIndex = _currentItemIndex; [self didScroll]; } From 6ed09f1c63694f1f81f1b26a1fc741cbbec6b0e2 Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Sat, 14 Jan 2017 02:06:14 +0900 Subject: [PATCH 07/10] Improvement gesture recognition between other custom gesture --- SwipeView/SwipeView.m | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/SwipeView/SwipeView.m b/SwipeView/SwipeView.m index f6f2fa1..5abf2ef 100755 --- a/SwipeView/SwipeView.m +++ b/SwipeView/SwipeView.m @@ -48,7 +48,6 @@ #error This class requires automatic reference counting #endif - @implementation NSObject (SwipeView) - (CGSize)swipeViewItemSize:(__unused SwipeView *)swipeView { return CGSizeZero; } @@ -64,10 +63,27 @@ - (void)swipeView:(__unused SwipeView *)swipeView didSelectItemAtIndex:(__unused @end +@interface SwipeScrollView : UIScrollView +@property (nonatomic, getter=isVertical) BOOL vertical; +@end + +@implementation SwipeScrollView + +- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { + if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) { + CGPoint velocity = [((UIPanGestureRecognizer *) gestureRecognizer) velocityInView:self]; + return _vertical ? ABS(velocity.y) < ABS(velocity.x) : ABS(velocity.x) > ABS(velocity.y); + } + return NO; +} + +@end + @interface SwipeView () @property (nonatomic, strong) UIScrollView *scrollView; +@property (nonatomic, strong) SwipeScrollView *castedScrollView; @property (nonatomic, strong) NSMutableDictionary *itemViews; @property (nonatomic, strong) NSMutableSet *itemViewPool; @property (nonatomic, assign) NSInteger previousItemIndex; @@ -85,7 +101,6 @@ @interface SwipeView () @end - @implementation SwipeView #pragma mark - @@ -103,7 +118,7 @@ - (void)setUp _defersItemViewLoading = NO; _vertical = NO; - _scrollView = [[UIScrollView alloc] init]; + _scrollView = [[SwipeScrollView alloc] init]; _scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; _scrollView.autoresizesSubviews = YES; _scrollView.delegate = self; @@ -287,6 +302,7 @@ - (void)setVertical:(BOOL)vertical _vertical = vertical; _scrollView.alwaysBounceHorizontal = !_vertical && _bounces; _scrollView.alwaysBounceVertical = _vertical && _bounces; + self.castedScrollView.vertical = _vertical; [self setNeedsLayout]; } } @@ -823,6 +839,11 @@ - (void)scrollByOffset:(CGFloat)offset duration:(NSTimeInterval)duration } } +- (SwipeScrollView *)castedScrollView +{ + return (SwipeScrollView *) _scrollView; +} + - (void)scrollToOffset:(CGFloat)offset duration:(NSTimeInterval)duration { [self scrollByOffset:[self minScrollDistanceFromOffset:_scrollOffset toOffset:offset] duration:duration]; From be5e686d11c785935aee19416b761d182db110ec Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Sun, 26 Feb 2017 00:35:17 +0900 Subject: [PATCH 08/10] Bug fix Fix bug is not called swipeViewCurrentItemIndexDidChange when the user swipes faster --- SwipeView/SwipeView.m | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/SwipeView/SwipeView.m b/SwipeView/SwipeView.m index 5abf2ef..659dd08 100755 --- a/SwipeView/SwipeView.m +++ b/SwipeView/SwipeView.m @@ -86,7 +86,7 @@ @interface SwipeView () @property (nonatomic, strong) SwipeScrollView *castedScrollView; @property (nonatomic, strong) NSMutableDictionary *itemViews; @property (nonatomic, strong) NSMutableSet *itemViewPool; -@property (nonatomic, assign) NSInteger previousItemIndex; +@property (nonatomic, assign) NSInteger selectedItemIndex; @property (nonatomic, assign) CGPoint previousContentOffset; @property (nonatomic, assign) CGSize itemSize; @property (nonatomic, assign) BOOL suppressScrollEvent; @@ -136,7 +136,7 @@ - (void)setUp _decelerationRate = _scrollView.decelerationRate; _itemViews = [[NSMutableDictionary alloc] init]; - _previousItemIndex = 0; + _selectedItemIndex = 0; _previousContentOffset = _scrollView.contentOffset; _scrollOffset = 0.0f; _currentItemIndex = 0; @@ -631,7 +631,6 @@ - (void)didScroll if (!_defersItemViewLoading || fabsf([self minScrollDistanceFromOffset:_lastUpdateOffset toOffset:_scrollOffset]) >= 1.0f) { - //update item index _currentItemIndex = [self clampedIndex:roundf(_scrollOffset)]; //load views @@ -789,8 +788,8 @@ - (CGFloat)minScrollDistanceFromOffset:(CGFloat)fromOffset toOffset:(CGFloat)toO - (void)setCurrentItemIndex:(NSInteger)currentItemIndex { - _previousItemIndex = _currentItemIndex; _currentItemIndex = currentItemIndex; + _selectedItemIndex = currentItemIndex; self.scrollOffset = currentItemIndex; } @@ -870,7 +869,7 @@ - (void)scrollByNumberOfItems:(NSInteger)itemCount duration:(NSTimeInterval)dura } else { - self.scrollOffset = [self clampedIndex:_previousItemIndex + itemCount]; + self.scrollOffset = [self clampedIndex:_currentItemIndex + itemCount]; } } @@ -1177,7 +1176,6 @@ - (void)scrollViewWillBeginDragging:(__unused UIScrollView *)scrollView //force refresh _lastUpdateOffset = self.scrollOffset - 1.0f; - _previousItemIndex = _currentItemIndex; [self didScroll]; } @@ -1213,9 +1211,12 @@ - (void)scrollViewDidEndDecelerating:(__unused UIScrollView *)scrollView [_delegate swipeViewDidEndDecelerating:self]; //send index update event - if (_previousItemIndex != _currentItemIndex) - { - _previousItemIndex = _currentItemIndex; + + NSInteger index = [self clampedIndex:roundf(_scrollOffset)]; + + if (index != _selectedItemIndex) { + _selectedItemIndex = index; + [_delegate swipeViewCurrentItemIndexDidChange:self]; } } From c23ec0ad842695f45c69df694ff918b4650d368e Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Thu, 2 Nov 2017 16:34:45 +0900 Subject: [PATCH 09/10] Add event point of selected index chaning --- SwipeView/SwipeView.m | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/SwipeView/SwipeView.m b/SwipeView/SwipeView.m index 659dd08..dc75e92 100755 --- a/SwipeView/SwipeView.m +++ b/SwipeView/SwipeView.m @@ -678,6 +678,7 @@ - (void)step else { [self stopAnimation]; + [self changeSelectedIndex]; } } @@ -1186,6 +1187,7 @@ - (void)scrollViewDidEndDragging:(__unused UIScrollView *)scrollView willDeceler //force refresh _lastUpdateOffset = self.scrollOffset - 1.0f; [self didScroll]; + [self changeSelectedIndex]; } [_delegate swipeViewDidEndDragging:self willDecelerate:decelerate]; } @@ -1209,9 +1211,10 @@ - (void)scrollViewDidEndDecelerating:(__unused UIScrollView *)scrollView [self didScroll]; [_delegate swipeViewDidEndDecelerating:self]; - - //send index update event - + [self changeSelectedIndex]; +} + +- (void)changeSelectedIndex { NSInteger index = [self clampedIndex:roundf(_scrollOffset)]; if (index != _selectedItemIndex) { From a26dc17100a0f0aa01f6489b642a03a02fcfe23c Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Thu, 2 Nov 2017 17:27:36 +0900 Subject: [PATCH 10/10] Bug fixed --- SwipeView/SwipeView.m | 1 + 1 file changed, 1 insertion(+) diff --git a/SwipeView/SwipeView.m b/SwipeView/SwipeView.m index dc75e92..9ebc5d4 100755 --- a/SwipeView/SwipeView.m +++ b/SwipeView/SwipeView.m @@ -836,6 +836,7 @@ - (void)scrollByOffset:(CGFloat)offset duration:(NSTimeInterval)duration else { self.scrollOffset += offset; + [self changeSelectedIndex]; } }