我根据用户点击屏幕的次数多次创建UIImageView
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.imageView];
fadeSpotRect = CGRectMake(touchPoint.x-20, touchPoint.y-20, 40, 40);
UIImageView *fadeSpotImageView = [[UIImageView alloc] initWithFrame:fadeSpotRect];
UIImage *fadeImage = [[UIImage alloc] init];
fadeImage = [filter smoothenPores:[self croppIngimageByImageName:faceImage toRect:CGRectMake(touchPoint.x-20, touchPoint.y-20, 40, 40)] intensity:10];
fadeImage = [self maskImage:fadeImage withMask:[UIImage imageNamed:@"fadeMask.png"]];
fadeSpotImageView.image = fadeImage;
[self.imageView addSubview:fadeSpotImageView];
}
现在如果我调用[fadeSpotImageView removeFromSuperview]它只删除我创建的最后一个UIImageView。我想删除我创建的所有UIImageView
。我怎么能这样做?
答案 0 :(得分:1)
一些选项 - 我更喜欢选项1,因为它是最明确,最干净,非常重要的文档本身。
1:声明NSMutableArray的属性,并使用其余的初始化代码对其进行分配和初始化。然后,每当您创建一个新的UIImageView并将其添加到您的数组时,例如
self.imageViews addObject: fadeSpotImageView];
然后当您准备好删除所有数据并通过数组进行枚举并将其全部删除时,请清除数组
[self.imageView enumerateObjectsUsingBlock:^(UIImageView *view, NSUInteger idx, BOOL *stop) {
[view removeFromSuperview];
}];
[self.imageViews removeAllObjects];
2:您可以盲目删除所有UIImageViews的子视图。这很简单,但您必须确保不删除您实际想要保留的视图。
[self.view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[UIImageView class]])
{
[obj removeFromSuperview];
}
}];
3:作为选项2的扩展,您可以在创建它们时标记所有imageView,然后将其用作循环中的条件检查。这样做的缺点是,现在您需要跟踪分配了视图的标记,并确保它与其他内容没有冲突(这种情况不太可能,但可能会发生)。
//put in your touchesBegan method
fadeSpotImageView.tag = 100 //arbitrary number chosen to id the imageview
//then put this as your condition
if (obj.tag == 100)
{
[obj removeFromSuperview];
}
答案 1 :(得分:0)
您可以使用快速枚举:
致电:[self removeImageViews];
- (void)removeImageViews
{
for (UIImageView *sub in self.imageView.subviews)
{
[sub removeFromSuperview];
}
}