I am beginner trying to make a menu using iCarousel. What I have tried so far is return a UIButton with a custom image for each view in carousel. The image list has been declared same as told in examples. Problems I am facing are:
button (view returned in carousel) - its image is not adjusting properly as shown in fig.
Also you can see that when I am displaying bear, it is picking description of Tiger!
When I tap any pic, it just scroll forward instead of reacting to button touch down event.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//set up carousel data
wrap = YES;
self.menuItems = [NSMutableArray arrayWithObjects:@"Bear.png",
@"Zebra.png",
@"Tiger.png",
@"Goat.png",
@"Birds.png",
@"Giraffe.png",
nil];
self.descriptions = [NSMutableArray arrayWithObjects:@"Bears Eat: Honey",
@"Zebras Eat: Grass",
@"Tigers Eat: Meat",
@"Goats Eat: Weeds",
@"Birds Eat: Seeds",
@"Giraffes Eat: Trees",
nil];
}
return self;
}
- (void)buttonTapped:(UIButton *)sender
{
NSInteger index = [aCarousel indexOfItemViewOrSubview:sender];
switch (index) {
case 0:
NSLog(@"at index: %d",index);
break;
case 1:
NSLog(@"at index: %d",index);
break;
case 2:
NSLog(@"at index: %d",index);
break;
case 3:
NSLog(@"at index: %d",index);
break;
case 4:
NSLog(@"at index: %d",index);
break;
case 5:
NSLog(@"at index: %d",index);
break;
} }
- (void)viewDidLoad
{
aCarousel.type = iCarouselTypeInvertedRotary;
aCarousel.delegate = self;
aCarousel.dataSource = self;
//aCarousel.frame = CGRectMake(0, 68, 320, 257);
[super viewDidLoad];
}
#pragma - mark iCarousel Delegate
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [self.menuItems count];
}
- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
// limit the number of item views loaded concurrently (for performance)
return 4;
}
- (UIView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake(0, 0, 240.0f, 200.0f);
[button setImage:[UIImage imageNamed:[menuItems objectAtIndex:index]] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchDown];
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
//button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
return button;
}
- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
return 0;
}
- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
// usually this should be slightly wider than the item views
return 240; //for 220 px image
}
- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
return self.wrap;
}
- (void)carouselDidScroll:(iCarousel *)carousel
{
[label setText:[NSString stringWithFormat:@"%@", [descriptions objectAtIndex:carousel.currentItemIndex]]];
}
I am really fed up trying adjusting carousel for hours. Although I tried aspectfitratio, adjusting frame but don't know why output is wrong. Can please someone figure out where am I doing wrong? Thanks alot :(
Atlast I was able to solve issue by myself. Don't know why my project was behaving so odd, so I decided to use iCarousel in new Project. The mistakes I noted were as follows:
In nib file, when I was making class type to iCarousel for subview, it was showing something like "I Carousel" in objects list - I changed the name in the list manually to my IBOutlet iCarousel object as "aCarousel".
Secondly I wasn't properly assigning UIButton to UIView in -(UIView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
method. I am glad that I found "Button demo" in Examples folder. From there I copied my code and now everything is working awesome! :)
Copying Code here for anybody who wants to see:
- (UIView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton *button = (UIButton *)view;
if (button == nil)
{
//no button available to recycle, so create new one
UIImage *image = [UIImage imageNamed:[menuItems objectAtIndex:index]];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
//[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
//button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchDown];
}
//set button label
//[button setTitle:[NSString stringWithFormat:@"%i", index] forState:UIControlStateNormal];
//Use these commented lines if you are displaying some text on button
return button;
}