gpt4 book ai didi

iphone - iCarousel iphone 中不同位置的图片如何实现删除功能

转载 作者:行者123 更新时间:2023-11-28 20:33:56 25 4
gpt4 key购买 nike

我正在使用如图所示的 iCarouselh here使用旋转木马类型 Liner Carousel 并实现了删除功能。我能够删除旋转木马中的图像,当我尝试删除可见屏幕中的任何其他图像时,它会移至旋转木马框架并删除。
我需要从原来的位置删除图像。

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

}

请帮帮我。

enter image description here

最佳答案

您不应该删除位于 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];
}

关于iphone - iCarousel iphone 中不同位置的图片如何实现删除功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11241139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com