diff --git a/iCarousel/iCarousel.h b/iCarousel/iCarousel.h index d49c268203..ec58ca957b 100644 --- a/iCarousel/iCarousel.h +++ b/iCarousel/iCarousel.h @@ -142,6 +142,10 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, readonly, getter = isDecelerating) BOOL decelerating; @property (nonatomic, readonly, getter = isScrolling) BOOL scrolling; +@property (nonatomic, assign) BOOL autoNextEnabled; +@property (nonatomic, assign) BOOL autoNextAnimate; +@property (nonatomic, assign) NSTimeInterval autoNextTimeInterval; + - (void)scrollByOffset:(CGFloat)offset duration:(NSTimeInterval)duration; - (void)scrollToOffset:(CGFloat)offset duration:(NSTimeInterval)duration; - (void)scrollByNumberOfItems:(NSInteger)itemCount duration:(NSTimeInterval)duration; @@ -160,6 +164,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)reloadData; +- (void)goToNextItemAnimated:(BOOL)animated; + @end diff --git a/iCarousel/iCarousel.m b/iCarousel/iCarousel.m index 48a6f32f74..a980101b4b 100644 --- a/iCarousel/iCarousel.m +++ b/iCarousel/iCarousel.m @@ -121,6 +121,7 @@ @interface iCarousel () @property (nonatomic, assign, getter = isDragging) BOOL dragging; @property (nonatomic, assign) BOOL didDrag; @property (nonatomic, assign) NSTimeInterval toggleTime; +@property (nonatomic, assign) NSTimer *autoNextTimer; NSComparisonResult compareViewDepth(UIView *view1, UIView *view2, iCarousel *self); @@ -147,6 +148,9 @@ - (void)setUp _scrollToItemBoundary = YES; _ignorePerpendicularSwipes = YES; _centerItemWhenSelected = YES; + _autoNextEnabled = NO; + _autoNextTimeInterval = 5; + _autoNextAnimate = YES; _contentView = [[UIView alloc] initWithFrame:self.bounds]; @@ -1648,6 +1652,8 @@ - (void)reloadItemAtIndex:(NSInteger)index animated:(BOOL)animated - (void)startAnimation { + [self resetAutoNextTimer]; + if (!_timer) { self.timer = [NSTimer timerWithTimeInterval:1.0/60.0 @@ -1917,7 +1923,42 @@ - (void)didScroll //update previous index _previousScrollOffset = _scrollOffset; _previousItemIndex = self.currentItemIndex; -} +} + + +#pragma mark - +#pragma mark Auto Next + +- (void) resetAutoNextTimer{ + if (_autoNextTimer != nil) { + [_autoNextTimer invalidate]; + _autoNextTimer = nil; + } + + if (_autoNextTimeInterval <= 0) { + _autoNextTimeInterval = 5; + } + + if (_autoNextEnabled && !_autoscroll) { + _autoNextTimer = [NSTimer scheduledTimerWithTimeInterval: _autoNextTimeInterval target: self + selector: @selector(controlAutoNext) userInfo: nil repeats: YES]; + } +} + +- (void)controlAutoNext{ + [self goToNextItemAnimated:_autoNextAnimate]; +} + +- (void)goToNextItemAnimated:(BOOL)animated{ + NSInteger currentPage = self.currentItemIndex; + NSInteger nextPage = self.currentItemIndex + 1; + + if (nextPage > _numberOfItems - 1) { + nextPage = 0; + } + + [self scrollToItemAtIndex:nextPage animated:animated]; +} #ifdef ICAROUSEL_IOS