iCarousel
iCarousel copied to clipboard
Not getting correct selected index
Hi,
I have issue in getting the correct selected index, I have used vertical infinite carousel view with type linear. In my my carousel currently visible items are 5 but when I do up and down scrolling, i don't get the correct middle selected index. Below is my code
Could you please suggest what I am doing wrong?
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.vCarousel.delegate = self;
self.vCarousel.dataSource = self;
self.vCarousel.type = iCarouselTypeLinear;
self.vCarousel.vertical = YES;
self.vCarousel.clipsToBounds = YES;
[self.vCarousel reloadData];
}
- (NSInteger)numberOfItemsInCarousel:(__unused iCarousel *)carousel
{
return (NSInteger)[self.items count];
}
- (UIView *)carousel:(__unused iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
UILabel *label = nil;
UIImageView *imageView = nil;
//create new view if no view is available for recycling
if (view==nil) {
view = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 105.0f, 140.0f)];
imageView = [[UIImageView alloc]initWithFrame:view
.bounds];
imageView.contentMode = UIViewContentModeCenter;
imageView.tag = 1;
[view addSubview:imageView];
}
else {
imageView = (UIImageView *)[view viewWithTag:1];
// numberLabel = (UILabel *)[view viewWithTag:1];
}
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[self.items objectAtIndex:index]]];
return view;
}
- (NSInteger)numberOfPlaceholdersInCarousel:(__unused iCarousel *)carousel
{
//note: placeholder views are only displayed on some carousels if wrapping is disabled
return 4;
}
- (UIView *)carousel:(__unused iCarousel *)carousel placeholderViewAtIndex:(NSInteger)index reusingView:(UIView *)view
{
UILabel *label = nil;
UIImageView *imageView = nil;
//create new view if no view is available for recycling
if (view==nil) {
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 105.0f, 140.0f)];
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 90.0f, 120.0f)];
imageView.contentMode = UIViewContentModeCenter;
imageView.tag = 1;
[view addSubview:imageView];
}
else {
imageView = (UIImageView *)[view viewWithTag:1];
// numberLabel = (UILabel *)[view viewWithTag:1];
}
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[self.items objectAtIndex:index]]];
//set item label
//remember to always set any properties of your carousel item
//views outside of the `if (view == nil) {...}` check otherwise
//you'll get weird issues with carousel item content appearing
//in the wrong place in the carousel
return view;
}
- (CATransform3D)carousel:(__unused iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
//implement 'flip3D' style carousel
transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * self.vCarousel.itemWidth);
}
- (CGFloat)carousel:(__unused iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
//customize carousel display
switch (option)
{
case iCarouselOptionWrap:
{
//normally you would hard-code this to YES or NO
return self.wrap;
}
case iCarouselOptionSpacing:
{
//add a bit of spacing between the item views
return value * 1.0f;
}
case iCarouselOptionFadeMax:
{
if (self.vCarousel.type == iCarouselTypeCustom)
{
//set opacity based on distance from camera
return 0.0f;
}
return value;
}
case iCarouselOptionShowBackfaces:
case iCarouselOptionRadius:
case iCarouselOptionAngle:
case iCarouselOptionArc:
case iCarouselOptionTilt:
case iCarouselOptionCount:
case iCarouselOptionFadeMin:
case iCarouselOptionFadeMinAlpha:
case iCarouselOptionFadeRange:
{
return value= 4.0f;
}
case iCarouselOptionOffsetMultiplier:
case iCarouselOptionVisibleItems:
{
return value = 5;
}
}
}
I'm not sure you are using placeholders correctly. it's a bit weird that you're setting the placeholder images to be images from your items array, as they are numbered differently. Did you mean to do that? What effect are you trying to achieve with the placeholders?
i face the same problem when use iCarousel.type = .Rotary didSelectItemAtIndex correctly at first but it fail after scrolling left & right
Same issue for me!
- (UIView *)carousel:(__unused iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
The index value is getting wrong! If I have four object to carousel its working fine. When I have five object in the carousel array the index value is not getting correct one. Your feedback will be appricated.
Hi guys!👋
I faced with the problem:
When user swipe up view I get wrong index in method:
- (void)swipedUp:(iCarousel *)carousel itemIndex:(NSInteger)index
And I fixed this problem as follows::
iCarousel.m -->
- (void)didPan:(UIPanGestureRecognizer *)panGesture
method -->
need to replace [_dataSource swipedUp:self itemIndex:self.currentItemIndex];
to -->
[_dataSource swipedUp:self itemIndex:[self indexOfItemView:[self itemViewAtPoint:[panGesture locationInView:_contentView]]]];
because method should get the index that user swiped, not self.currentItemIndex!
Hope this can help someone in the future 🤓🙌🏻