0

ここに示すように、カルーセル タイプのライナー カルーセルでiCarousel を使用し 、削除機能を実装しています。カルーセル内の画像を削除することができ、表示されている画面内の他の画像を削除しようとすると、カルーセル フレームに移動して削除されます。
画像を元の位置から削除する必要があります。

- (void)loadView {

    [super loadView];

    self.view.backgroundColor = [UIColor blackColor];
    
    carousel = [[iCarousel alloc] initWithFrame:CGRectMake(-130,300, 320, 100)];
    carousel.dataSource = self;
    carousel.delegate=self;
    carousel.type = iCarouselTypeLinear;
    carousel.scrollEnabled=YES;

    imageView=[[UIImageView alloc]initWithFrame:CGRectMake(60, 50, 200, 200)];
    [self.view addSubview:imageView];


}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
    { 

    UIImage *image = [imagesArray objectAtIndex:index];

    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0,0, 60, 60); 
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    button.tag=index;
    NSLog(@"tag is %d",button.tag);
        
    UIButton *deleteButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    deleteButton.frame= CGRectMake(50, -5, 20 ,20);
    UIImage *img = [UIImage imageNamed:@"close.png"];
    [deleteButton setImage:img forState:UIControlStateNormal];
    [deleteButton addTarget:self action:@selector(deleteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [button addSubview:deleteButton];
        
    return button;
}

-(void)deleteButtonClicked:(int )sender
{
    NSInteger currentIndex = carousel.currentItemIndex;
    [carousel removeItemAtIndex:currentIndex animated:YES];
   
}

私を助けてください。

ここに画像の説明を入力

4

2 に答える 2

2

carousel.currentItemIndex のアイテムは削除しないでください。これは、クリックしたボタンに対応するアイテムではなく、現在カルーセルの中央に配置されているアイテムだからです。

クリックしたボタンの正しい項目インデックスを取得するには、次のようにします。

-(void)deleteButtonClicked:(id)sender
{
    //get the index of the item view containing the button that was clicked
    NSInteger index = [carousel indexOfItemViewOrSubview:sender];

    //update the data model (always do this first)
    [imagesArray removeObjectAtIndex:index];

    //remove item from carousel
    [carousel removeItemAtIndex:index animated:YES];
}
于 2012-06-28T12:47:23.423 に答える
1

これを試して:

    NSInteger index = carousel.currentItemIndex;
    [carousel removeItemAtIndex:index animated:YES];
    [imagesArray removeObjectAtIndex:index];

これも追加します:

- (void)awakeFromNib
{    
    if (self) {
        //set up carousel data
         wrap = NO;
    }
}

またはあなたの

carousel.type = iCarouselTypeCustom;

carousel.type = iCarouselTypeLinear;

次に、これを実装します。[carousel reloadData];

于 2012-06-28T09:16:10.987 に答える