UIButton ImageView在iCarousel中占用更多空间而不响应触摸事件

时间:2012-11-18 11:43:03

标签: ios5 uiimageview uibutton icarousel

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

  1. 按钮(轮播中返回的视图) - 其图像未正确调整,如图所示

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

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

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

1 个答案:

答案 0 :(得分:0)

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

  1. 在nib文件中,当我在iCarousel中为子视图创建类类型时,它在对象列表中显示类似“I Carousel”的内容 - 我更改了列表中的名称手动输入我的 IBOutlet iCarousel对象作为“aCarousel”。

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

  3. 此处为任何想要查看的人复制代码:

    - (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;
    }