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

Posted

技术标签:

【中文标题】UIButton ImageView 在 iCarousel 中占用更多空间并且不响应触摸事件【英文标题】:UIButton ImageView Taking More Space in iCarousel and not Responding to touch Event 【发布时间】:2012-11-18 11:43:03 【问题描述】:

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

【问题讨论】:

【参考方案1】:

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

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

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

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

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

【讨论】:

以上是关于UIButton ImageView 在 iCarousel 中占用更多空间并且不响应触摸事件的主要内容,如果未能解决你的问题,请参考以下文章

如何缩放 UIButton 的 imageView?

UIButton 背景 VS UIButton imageview.view

根据状态更改 UIButton 中 ImageView 的 tint 颜色

swift 在代码中将生成的imageView变成UIButton

摆脱 UITableViewCell 中 UIButton 上的 imageView 色调

在 UIButton 的 ImageView 上将 ContentMode 设置为 AspectFill 不适用于较小的图像