2

我有 6 个按钮,我想使用 iCarousel 制作动画,代码如下

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton *button = (UIButton *)view;
if (button == nil)
{
    self.icon = [NSMutableArray arrayWithObjects:@"icon-02.png",@"icon-03.png",@"icon-04.png",@"icon-05.png",@"icon-06.png",@"icon-07.png",nil];


    //no button available to recycle, so create new one
    UIImage *image = [UIImage imageNamed:[icon objectAtIndex:index]];
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0.0, 0.0, 130.0f, 130.0f);
    [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:UIControlEventTouchUpInside];
}



return button;
}

但是按钮不在中心,现在有没有人热心让旋转木马在中心,我已经调整了 uiview 的大小但仍然无法正常工作。谢谢...

4

1 回答 1

3

carousel 和 carousel 项目默认应该居中(它们在 iCarousel 包含的示例项目中,它们没有做任何特别的事情)。无需调整笔尖中旋转木马的位置(除非它没有明显居中)。如果这没有按预期工作,您可能发现了一个错误 - 您可以在项目的 github 页面上提出问题吗?

无关:您那里的按钮回收逻辑完全错误,只是巧合。除其他事项外,您正在重新创建图标数组 6 次。

创建按钮的正确方法是这样的:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //set up icons array
     self.icon = [NSMutableArray arrayWithObjects:@"icon-02.png",@"icon-03.png",@"icon-04.png",@"icon-05.png",@"icon-06.png",@"icon-07.png",nil];
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    UIButton *button = (UIButton *)view;
    if (button == nil)
    {
        //*************************************************
        //do setup that is the same for every button here
        //*************************************************

        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0.0, 0.0, 130.0f, 130.0f);
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
        //[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    }

    //*************************************************
    //do setup that is different depending on index here
    //*************************************************

    UIImage *image = [UIImage imageNamed:[icon objectAtIndex:index]];
    [button setBackgroundImage:image forState:UIControlStateNormal];

    return button;
}
于 2012-11-21T07:54:31.313 回答