Skip to content
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
7 changes: 7 additions & 0 deletions Examples/Basic iOS Example/iCarouselExampleViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ - (void)viewDidLoad
_carousel.type = iCarouselTypeCoverFlow2;
}

-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

// Uncomment this line for testing autoscrolling.
// [_carousel start];
}

- (void)viewDidUnload
{
[super viewDidUnload];
Expand Down
13 changes: 13 additions & 0 deletions iCarousel/iCarousel.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly, getter = isDragging) BOOL dragging;
@property (nonatomic, readonly, getter = isDecelerating) BOOL decelerating;
@property (nonatomic, readonly, getter = isScrolling) BOOL scrolling;
@property (nonatomic, assign) CGFloat autoscrollDelay;

- (void)scrollByOffset:(CGFloat)offset duration:(NSTimeInterval)duration;
- (void)scrollToOffset:(CGFloat)offset duration:(NSTimeInterval)duration;
Expand All @@ -158,6 +159,12 @@ NS_ASSUME_NONNULL_BEGIN
- (void)insertItemAtIndex:(NSInteger)index animated:(BOOL)animated;
- (void)reloadItemAtIndex:(NSInteger)index animated:(BOOL)animated;

// Autoscroll methods
- (void)start;
- (void)stop;
- (void)previous;
- (void)next;

- (void)reloadData;

@end
Expand Down Expand Up @@ -195,6 +202,12 @@ NS_ASSUME_NONNULL_BEGIN
- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform;
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value;

// Autoscroll delegates
- (void)carouselDidShowNext:(iCarousel *)carousel;
- (void)carouselDidShowPrevious:(iCarousel *)carousel;
- (void)carouselWillShowNext:(iCarousel *)carousel;
- (void)carouselWillShowPrevious:(iCarousel *)carousel;

@end

NS_ASSUME_NONNULL_END
Expand Down
89 changes: 88 additions & 1 deletion iCarousel/iCarousel.m
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ @interface iCarousel ()
@property (nonatomic, assign) BOOL didDrag;
@property (nonatomic, assign) NSTimeInterval toggleTime;

@property (atomic) BOOL doStop;
@property (atomic) BOOL isAnimating;

NSComparisonResult compareViewDepth(UIView *view1, UIView *view2, iCarousel *self);

@end
Expand All @@ -147,7 +150,11 @@ - (void)setUp
_scrollToItemBoundary = YES;
_ignorePerpendicularSwipes = YES;
_centerItemWhenSelected = YES;


_doStop = YES;
_isAnimating = NO;
_autoscrollDelay = 2;

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


Expand Down Expand Up @@ -1768,6 +1775,13 @@ - (void)step
[self pushAnimationState:YES];
[_delegate carouselDidEndScrollingAnimation:self];
[self popAnimationState];

_isAnimating = NO;

if (!_doStop) {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(next) object:nil];
[self performSelector:@selector(next) withObject:nil afterDelay:_autoscrollDelay];
}
}
}
else if (_decelerating)
Expand Down Expand Up @@ -2341,4 +2355,77 @@ - (void)keyDown:(NSEvent *)theEvent

#endif


#pragma mark - Autoscroll methods

- (void)start
{
_doStop = NO;
[self next];
}

- (void)stop
{
_doStop = YES;
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(next) object:nil];
}

- (void)next
{
if (!_isAnimating && (self.dataSource && _numberOfItems >1)) {

if ([self.delegate respondsToSelector:@selector(carouselWillShowNext:)]) {
[self.delegate carouselWillShowNext:self];
}

// Next Image
NSUInteger nextIndex = (self.currentItemIndex+1) % _numberOfItems;

// Animate
[self scrollToItemAtIndex:nextIndex animated:YES];

// Call delegate
if([self.delegate respondsToSelector:@selector(carouselDidShowNext:)]){
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, SCROLL_DURATION * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if (self.window){
[self.delegate carouselDidShowNext:self];
}
});
}
}
}

- (void)previous
{
if (!_isAnimating && (self.dataSource && _numberOfItems >1)) {

if ([self.delegate respondsToSelector:@selector(carouselWillShowPrevious:)]) {
[self.delegate carouselWillShowPrevious:self];
}

// Previous image
NSUInteger prevIndex;
if (self.currentItemIndex == 0){
prevIndex = _numberOfItems - 1;
} else {
prevIndex = (self.currentItemIndex - 1) % _numberOfItems;
}

// Animate
[self scrollToItemAtIndex:prevIndex animated:YES];

// Call delegate
if ([self.delegate respondsToSelector:@selector(carouselDidShowPrevious:)]) {
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, SCROLL_DURATION * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
if (self.window) {
[self.delegate carouselDidShowPrevious:self];
}
});
}
}

}

@end