2

iCarousel を使用してアニメーション化したい 6 つのボタンがあります。このようなコードです。

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

カルーセルとカルーセル アイテムはデフォルトで中央に配置する必要があります (これらは 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 に答える