Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change event call point from scrollViewDidScroll to scrollViewDidEndDecelerating for swipeViewCurrentItemIndexDidChange #205

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions SwipeView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions SwipeView/SwipeView.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ typedef NS_ENUM(NSUInteger, SwipeViewAlignment)

@interface SwipeView : UIView

@property (nonatomic, weak_delegate) IBOutlet id<SwipeViewDataSource> dataSource;
@property (nonatomic, weak_delegate) IBOutlet id<SwipeViewDelegate> delegate;
@property (nonatomic, weak) IBOutlet id<SwipeViewDataSource> dataSource;
@property (nonatomic, weak) IBOutlet id<SwipeViewDelegate> delegate;
@property (nonatomic, readonly) NSInteger numberOfItems;
@property (nonatomic, readonly) NSInteger numberOfPages;
@property (nonatomic, readonly) CGSize itemSize;
Expand All @@ -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, readonly) UIScrollView *scrollView;

- (void)reloadData;
- (void)reloadItemAtIndex:(NSInteger)index;
Expand Down
70 changes: 49 additions & 21 deletions SwipeView/SwipeView.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
#error This class requires automatic reference counting
#endif


@implementation NSObject (SwipeView)

- (CGSize)swipeViewItemSize:(__unused SwipeView *)swipeView { return CGSizeZero; }
Expand All @@ -64,13 +63,30 @@ - (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 () <UIScrollViewDelegate, UIGestureRecognizerDelegate>

@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;
@property (nonatomic, assign) NSInteger selectedItemIndex;
@property (nonatomic, assign) CGPoint previousContentOffset;
@property (nonatomic, assign) CGSize itemSize;
@property (nonatomic, assign) BOOL suppressScrollEvent;
Expand All @@ -85,7 +101,6 @@ @interface SwipeView () <UIScrollViewDelegate, UIGestureRecognizerDelegate>

@end


@implementation SwipeView

#pragma mark -
Expand All @@ -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;
Expand All @@ -121,7 +136,7 @@ - (void)setUp

_decelerationRate = _scrollView.decelerationRate;
_itemViews = [[NSMutableDictionary alloc] init];
_previousItemIndex = 0;
_selectedItemIndex = 0;
_previousContentOffset = _scrollView.contentOffset;
_scrollOffset = 0.0f;
_currentItemIndex = 0;
Expand Down Expand Up @@ -273,7 +288,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];
Expand All @@ -287,6 +302,7 @@ - (void)setVertical:(BOOL)vertical
_vertical = vertical;
_scrollView.alwaysBounceHorizontal = !_vertical && _bounces;
_scrollView.alwaysBounceVertical = _vertical && _bounces;
self.castedScrollView.vertical = _vertical;
[self setNeedsLayout];
}
}
Expand Down Expand Up @@ -391,11 +407,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)];
}
Expand Down Expand Up @@ -613,21 +629,13 @@ - (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)];

//load views
_lastUpdateOffset = _currentItemIndex;
[self loadUnloadViews];

//send index update event
if (_previousItemIndex != _currentItemIndex)
{
_previousItemIndex = _currentItemIndex;
[_delegate swipeViewCurrentItemIndexDidChange:self];
}
}
}

Expand Down Expand Up @@ -670,6 +678,7 @@ - (void)step
else
{
[self stopAnimation];
[self changeSelectedIndex];
}
}

Expand Down Expand Up @@ -773,14 +782,15 @@ - (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;
}

- (void)setCurrentItemIndex:(NSInteger)currentItemIndex
{
_currentItemIndex = currentItemIndex;
_selectedItemIndex = currentItemIndex;
self.scrollOffset = currentItemIndex;
}

Expand All @@ -794,7 +804,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
Expand Down Expand Up @@ -826,9 +836,15 @@ - (void)scrollByOffset:(CGFloat)offset duration:(NSTimeInterval)duration
else
{
self.scrollOffset += offset;
[self changeSelectedIndex];
}
}

- (SwipeScrollView *)castedScrollView
{
return (SwipeScrollView *) _scrollView;
}

- (void)scrollToOffset:(CGFloat)offset duration:(NSTimeInterval)duration
{
[self scrollByOffset:[self minScrollDistanceFromOffset:_scrollOffset toOffset:offset] duration:duration];
Expand All @@ -855,7 +871,7 @@ - (void)scrollByNumberOfItems:(NSInteger)itemCount duration:(NSTimeInterval)dura
}
else
{
self.scrollOffset = [self clampedIndex:_previousItemIndex + itemCount];
self.scrollOffset = [self clampedIndex:_currentItemIndex + itemCount];
}
}

Expand Down Expand Up @@ -1172,6 +1188,7 @@ - (void)scrollViewDidEndDragging:(__unused UIScrollView *)scrollView willDeceler
//force refresh
_lastUpdateOffset = self.scrollOffset - 1.0f;
[self didScroll];
[self changeSelectedIndex];
}
[_delegate swipeViewDidEndDragging:self willDecelerate:decelerate];
}
Expand All @@ -1185,7 +1202,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;
}
Expand All @@ -1195,6 +1212,17 @@ - (void)scrollViewDidEndDecelerating:(__unused UIScrollView *)scrollView
[self didScroll];

[_delegate swipeViewDidEndDecelerating:self];
[self changeSelectedIndex];
}

- (void)changeSelectedIndex {
NSInteger index = [self clampedIndex:roundf(_scrollOffset)];

if (index != _selectedItemIndex) {
_selectedItemIndex = index;

[_delegate swipeViewCurrentItemIndexDidChange:self];
}
}

@end