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

Use -respondsToSelector: for delegate testing #906

Open
wants to merge 2 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion iCarousel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
);
path = iCarousel;
sourceTree = "<group>";
usesTabs = 0;
};
8215073B1E5746F400C75711 /* Supporting Files */ = {
isa = PBXGroup;
Expand Down Expand Up @@ -150,7 +151,7 @@
};
buildConfigurationList = 821507251E57468200C75711 /* Build configuration list for PBXProject "iCarousel" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Expand Down Expand Up @@ -404,6 +405,7 @@
821507491E57475B00C75711 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
161 changes: 96 additions & 65 deletions iCarousel/iCarousel.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,33 +73,6 @@
#endif


@implementation NSObject (iCarousel)

- (NSUInteger)numberOfPlaceholdersInCarousel:(__unused iCarousel *)carousel { return 0; }
- (void)carouselWillBeginScrollingAnimation:(__unused iCarousel *)carousel {}
- (void)carouselDidEndScrollingAnimation:(__unused iCarousel *)carousel {}
- (void)carouselDidScroll:(__unused iCarousel *)carousel {}

- (void)carouselCurrentItemIndexDidChange:(__unused iCarousel *)carousel {}
- (void)carouselWillBeginDragging:(__unused iCarousel *)carousel {}
- (void)carouselDidEndDragging:(__unused iCarousel *)carousel willDecelerate:(__unused BOOL)decelerate {}
- (void)carouselWillBeginDecelerating:(__unused iCarousel *)carousel {}
- (void)carouselDidEndDecelerating:(__unused iCarousel *)carousel {}

- (BOOL)carousel:(__unused iCarousel *)carousel shouldSelectItemAtIndex:(__unused NSInteger)index { return YES; }
- (void)carousel:(__unused iCarousel *)carousel didSelectItemAtIndex:(__unused NSInteger)index {}

- (CGFloat)carouselItemWidth:(__unused iCarousel *)carousel { return 0; }
- (CATransform3D)carousel:(__unused iCarousel *)carousel
itemTransformForOffset:(__unused CGFloat)offset
baseTransform:(CATransform3D)transform { return transform; }
- (CGFloat)carousel:(__unused iCarousel *)carousel
valueForOption:(__unused iCarouselOption)option
withDefault:(CGFloat)value { return value; }

@end


@interface iCarousel ()

@property (nonatomic, strong) UIView *contentView;
Expand Down Expand Up @@ -497,8 +470,12 @@ - (CGFloat)alphaForItemWithOffset:(CGFloat)offset
}

- (CGFloat)valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
return _delegate? [_delegate carousel:self valueForOption:option withDefault:value]: value;
{
if ([_delegate respondsToSelector:@selector(carousel:valueForOption:withDefault:)])
{
return [_delegate carousel:self valueForOption:option withDefault:value];
}
return value;
}

- (CATransform3D)transformForItemViewWithOffset:(CGFloat)offset
Expand All @@ -513,7 +490,14 @@ - (CATransform3D)transformForItemViewWithOffset:(CGFloat)offset
{
case iCarouselTypeCustom:
{
return [_delegate carousel:self itemTransformForOffset:offset baseTransform:transform];
if ([_delegate respondsToSelector:@selector(carousel:itemTransformForOffset:baseTransform:)])
{
return [_delegate carousel:self itemTransformForOffset:offset baseTransform:transform];
}
else
{
return transform;
}
}
case iCarouselTypeLinear:
{
Expand Down Expand Up @@ -933,7 +917,10 @@ - (void)transformItemViews

- (void)updateItemWidth
{
_itemWidth = [_delegate carouselItemWidth:self] ?: _itemWidth;
if ([_delegate respondsToSelector:@selector(carouselItemWidth:)])
{
_itemWidth = [_delegate carouselItemWidth:self] ?: _itemWidth;
}
if (_numberOfItems > 0)
{
if ([_itemViews count] == 0)
Expand Down Expand Up @@ -1351,7 +1338,14 @@ - (void)reloadData
//get number of items and placeholders
_numberOfVisibleItems = 0;
_numberOfItems = [_dataSource numberOfItemsInCarousel:self];
_numberOfPlaceholders = [_dataSource numberOfPlaceholdersInCarousel:self];
if ([_dataSource respondsToSelector:@selector(numberOfPlaceholdersInCarousel:)])
{
_numberOfPlaceholders = [_dataSource numberOfPlaceholdersInCarousel:self];
}
else
{
_numberOfPlaceholders = 0;
}

//reset view pools
self.itemViews = [NSMutableDictionary dictionary];
Expand Down Expand Up @@ -1453,7 +1447,10 @@ - (void)scrollByOffset:(CGFloat)offset duration:(NSTimeInterval)duration
{
_endOffset = [self clampedOffset:_endOffset];
}
[_delegate carouselWillBeginScrollingAnimation:self];
if ([_delegate respondsToSelector:@selector(carouselWillBeginScrollingAnimation:)])
{
[_delegate carouselWillBeginScrollingAnimation:self];
}
[self startAnimation];
}
else
Expand Down Expand Up @@ -1770,9 +1767,12 @@ - (void)step
{
_scrolling = NO;
[self depthSortViews];
[self pushAnimationState:YES];
[_delegate carouselDidEndScrollingAnimation:self];
[self popAnimationState];
if ([_delegate respondsToSelector:@selector(carouselDidEndScrollingAnimation:)])
{
[self pushAnimationState:YES];
[_delegate carouselDidEndScrollingAnimation:self];
[self popAnimationState];
}
}
}
else if (_decelerating)
Expand All @@ -1785,9 +1785,12 @@ - (void)step
if (fabs(time - _scrollDuration) < FLOAT_ERROR_MARGIN)
{
_decelerating = NO;
[self pushAnimationState:YES];
[_delegate carouselDidEndDecelerating:self];
[self popAnimationState];
if ([_delegate respondsToSelector:@selector(carouselDidEndDecelerating:)])
{
[self pushAnimationState:YES];
[_delegate carouselDidEndDecelerating:self];
[self popAnimationState];
}
if ((_scrollToItemBoundary || fabs(_scrollOffset - [self clampedOffset:_scrollOffset]) > FLOAT_ERROR_MARGIN) && !_autoscroll)
{
if (fabs(_scrollOffset - self.currentItemIndex) < FLOAT_ERROR_MARGIN)
Expand Down Expand Up @@ -1908,17 +1911,23 @@ - (void)didScroll
//notify delegate of offset change
if (fabs(_scrollOffset - _previousScrollOffset) > FLOAT_ERROR_MARGIN)
{
[self pushAnimationState:YES];
[_delegate carouselDidScroll:self];
[self popAnimationState];
if ([_delegate respondsToSelector:@selector(carouselDidScroll:)])
{
[self pushAnimationState:YES];
[_delegate carouselDidScroll:self];
[self popAnimationState];
}
}

//notify delegate of index change
if (_previousItemIndex != self.currentItemIndex)
{
[self pushAnimationState:YES];
[_delegate carouselCurrentItemIndexDidChange:self];
[self popAnimationState];
if ([_delegate respondsToSelector:@selector(carouselCurrentItemIndexDidChange:)])
{
[self pushAnimationState:YES];
[_delegate carouselCurrentItemIndexDidChange:self];
[self popAnimationState];
}
}

//update previous index
Expand Down Expand Up @@ -2072,14 +2081,16 @@ - (void)didTap:(UITapGestureRecognizer *)tapGesture
NSInteger index = itemView? [self indexOfItemView:itemView]: NSNotFound;
if (index != NSNotFound)
{
if (!_delegate || [_delegate carousel:self shouldSelectItemAtIndex:index])
if (![_delegate respondsToSelector:@selector(carousel:shouldSelectItemAtIndex:)] || [_delegate carousel:self shouldSelectItemAtIndex:index])
{
if ((index != self.currentItemIndex && _centerItemWhenSelected) ||
(index == self.currentItemIndex && _scrollToItemBoundary))
{
[self scrollToItemAtIndex:index animated:YES];
}
[_delegate carousel:self didSelectItemAtIndex:index];
if ([_delegate respondsToSelector:@selector(carousel:didSelectItemAtIndex:)]) {
[_delegate carousel:self didSelectItemAtIndex:index];
}
}
else if (_scrollEnabled && _scrollToItemBoundary && _autoscroll)
{
Expand Down Expand Up @@ -2110,7 +2121,10 @@ - (void)didPan:(UIPanGestureRecognizer *)panGesture
_previousTranslation = -_previousTranslation;
#endif

[_delegate carouselWillBeginDragging:self];
if ([_delegate respondsToSelector:@selector(carouselWillBeginDragging:)])
{
[_delegate carouselWillBeginDragging:self];
}
break;
}
case UIGestureRecognizerStateEnded:
Expand All @@ -2125,9 +2139,12 @@ - (void)didPan:(UIPanGestureRecognizer *)panGesture
[self startDecelerating];
}

[self pushAnimationState:YES];
[_delegate carouselDidEndDragging:self willDecelerate:_decelerating];
[self popAnimationState];
if ([_delegate respondsToSelector:@selector(carouselDidEndDragging:willDecelerate:)])
{
[self pushAnimationState:YES];
[_delegate carouselDidEndDragging:self willDecelerate:_decelerating];
[self popAnimationState];
}

if (!_decelerating)
{
Expand Down Expand Up @@ -2156,9 +2173,12 @@ - (void)didPan:(UIPanGestureRecognizer *)panGesture
}
else
{
[self pushAnimationState:YES];
[_delegate carouselWillBeginDecelerating:self];
[self popAnimationState];
if ([_delegate respondsToSelector:@selector(carouselWillBeginDecelerating:)])
{
[self pushAnimationState:YES];
[_delegate carouselWillBeginDecelerating:self];
[self popAnimationState];
}
}
break;
}
Expand Down Expand Up @@ -2215,7 +2235,10 @@ - (void)mouseDragged:(NSEvent *)theEvent
if (!_dragging)
{
_dragging = YES;
[_delegate carouselWillBeginDragging:self];
if ([_delegate respondsToSelector:@selector(carouselWillBeginDragging:)])
{
[_delegate carouselWillBeginDragging:self];
}
}
_scrolling = NO;
_decelerating = NO;
Expand Down Expand Up @@ -2255,11 +2278,13 @@ - (void)mouseUp:(NSEvent *)theEvent
{
[self scrollToItemAtIndex:index animated:YES];
}
if (!_delegate || [_delegate carousel:self shouldSelectItemAtIndex:index])
if (![_delegate respondsToSelector:@selector(carousel:shouldSelectItemAtIndex:)] || [_delegate carousel:self shouldSelectItemAtIndex:index])
{
[self pushAnimationState:YES];
[_delegate carousel:self didSelectItemAtIndex:index];
[self popAnimationState];
if ([_delegate respondsToSelector:@selector(carousel:didSelectItemAtIndex:)]) {
[self pushAnimationState:YES];
[_delegate carousel:self didSelectItemAtIndex:index];
[self popAnimationState];
}
}
}
}
Expand All @@ -2272,9 +2297,12 @@ - (void)mouseUp:(NSEvent *)theEvent
[self startDecelerating];
}

[self pushAnimationState:YES];
[_delegate carouselDidEndDragging:self willDecelerate:_decelerating];
[self popAnimationState];
if ([_delegate respondsToSelector:@selector(carouselDidEndDragging:willDecelerate:)])
{
[self pushAnimationState:YES];
[_delegate carouselDidEndDragging:self willDecelerate:_decelerating];
[self popAnimationState];
}

if (!_decelerating && !_autoscroll)
{
Expand All @@ -2290,9 +2318,12 @@ - (void)mouseUp:(NSEvent *)theEvent
}
else
{
[self pushAnimationState:YES];
[_delegate carouselWillBeginDecelerating:self];
[self popAnimationState];
if ([_delegate respondsToSelector:@selector(carouselWillBeginDecelerating:)])
{
[self pushAnimationState:YES];
[_delegate carouselWillBeginDecelerating:self];
[self popAnimationState];
}
}
}
}
Expand Down