如何集中iCarousel视图

时间:2012-11-21 04:44:10

标签: iphone ios icarousel

我有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的大小,但仍然没有工作。 感谢...

1 个答案:

答案 0 :(得分:3)

轮播和轮播项目​​应默认居中(它们位于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;
}