我是初学者,尝试使用iCarousel制作菜单。到目前为止,我尝试的是为轮播中的每个视图返回一个带有自定义图像的 UIButton。图像列表已声明与示例中的说明相同。我面临的问题是:
按钮(在轮播中返回视图) - 它的图像没有正确调整,如图所示。
您还可以看到,当我显示熊时,它正在挑选老虎的描述!
当我点击任何图片时,它只是向前滚动而不是对按钮按下事件做出反应。
- (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]]];
}
我真的厌倦了尝试调整轮播数小时。虽然我尝试了aspectfitratio,调整框架但不知道为什么输出错误。可以请有人弄清楚我在哪里做错了吗?非常感谢 :(