1

我是初学者,尝试使用iCarousel制作菜单。到目前为止,我尝试的是为轮播中的每个视图返回一个带有自定义图像的 UIButton。图像列表已声明与示例中的说明相同。我面临的问题是:

  1. 按钮(在轮播中返回视图) - 它的图像没有正确调整,如图所示。

  2. 您还可以看到,当我显示熊时,它正在挑选老虎的描述!

  3. 当我点击任何图片时,它只是向前滚动而不是对按钮按下事件做出反应。

- (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,调整框架但不知道为什么输出错误。可以请有人弄清楚我在哪里做错了吗?非常感谢 :(

4

1 回答 1

0

Atlast 我能够自己解决问题。不知道为什么我的项目表现得如此奇怪,所以我决定在新项目中使用 iCarousel。我注意到的错误如下:

  1. 在 nib 文件中,当我将类类型设置为 iCarousel 用于子视图时,它在对象列表中显示类似“I Carousel”的内容- 我将列表中的名称手动更改为我的IBOutlet iCarousel 对象为“aCarousel”。

  2. 其次,我没有在-(UIView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view方法中正确地将 UIButton 分配给 UIView 。我很高兴在示例文件夹中找到“按钮演示” 。从那里我复制了我的代码,现在一切都很棒!:)

在这里复制代码给任何想看的人:

- (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;
}
于 2012-11-18T18:41:35.747 回答