iphone - 如何使 iCarousel View 居中

标签 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 的大小,但仍然无法工作。 谢谢...

最佳答案

默认情况下,轮播和轮播项目​​应居中(它们位于 iCarousel 附带的示例项目中,它们没有做任何特殊的事情)。无需调整 Nib 中转盘的位置(除非它没有明显居中)。如果这没有按预期工作,您可能发现了一个错误 - 您可以在项目的 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;
}

关于iphone - 如何使 iCarousel View 居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13486473/

相关文章:

iphone - 用于在 Mac 上全新安装的最新 XCode .dmg(安装程序)

iphone - 这是使用 AVAudioPlayer 的正确方法吗?我的代码看起来正确吗?

iphone - iOS:如何查询nsmutablearray对象中的对象

ios - 在扩展中添加和删除 Notification Observer

iphone - 无法使用 CALayer for iphone 创建 ken burns 效果

ios - 在 Xcode 中使用 bash 的编译标志

ios - didSelectRowAtIndexPath 和 prepareForSegue 实现

ios - 如何用动画重新加载 iCarousel?

ios - iCarousel 拉动刷新并加载更多

iphone - 如何在 ios 4 中以编程方式将图像数组添加到 Imageview?