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

Add Auto move to next item after time #656

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions iCarousel/iCarousel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -160,6 +164,8 @@ NS_ASSUME_NONNULL_BEGIN

- (void)reloadData;

- (void)goToNextItemAnimated:(BOOL)animated;

@end


Expand Down
43 changes: 42 additions & 1 deletion iCarousel/iCarousel.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -147,6 +148,9 @@ - (void)setUp
_scrollToItemBoundary = YES;
_ignorePerpendicularSwipes = YES;
_centerItemWhenSelected = YES;
_autoNextEnabled = NO;
_autoNextTimeInterval = 5;
_autoNextAnimate = YES;

_contentView = [[UIView alloc] initWithFrame:self.bounds];

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down