xcode 从视图中删除一些子视图
大家好,
我是一个菜鸟,几天来我一直在努力解决这个问题。
我正在通过 UItouch 将图像添加到视图中。该视图包含一个背景,在该背景上添加新图像。如何清除从子视图添加的图像,而不删除作为背景的 UIImage。非常感谢任何帮助。提前致谢。
这是代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {
NSUInteger numTaps = [[touches anyObject] tapCount];
if (numTaps==2) {
imageCounter.text =@"two taps registered";
//__ remove images
UIView* subview;
while ((subview = [[self.view subviews] lastObject]) != nil)
[subview removeFromSuperview];
return;
}else {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
CGRect myImageRect = CGRectMake((touchPoint.x -40), (touchPoint.y -45), 80.0f, 90.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"pg6_dog_button.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
[imagesArray addObject:myImage];
NSNumber *arrayCount =[self.view.subviews count];
viewArrayCount.text =[NSString stringWithFormat:@"%d",arrayCount];
imageCount=imageCount++;
imageCounter.text =[NSString stringWithFormat:@"%d",imageCount];
}
}
Greetings all,
I am a noob and I have been trying to work through this for a few days.
I am adding images to a view via UItouch. The view contains a background on top of which the new images are add. How do I clear the images I am adding from the subview, without getting rid of the UIImage that is the background. Any assistance is greatly appreciated. Thanks in Advance.
here is the code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {
NSUInteger numTaps = [[touches anyObject] tapCount];
if (numTaps==2) {
imageCounter.text =@"two taps registered";
//__ remove images
UIView* subview;
while ((subview = [[self.view subviews] lastObject]) != nil)
[subview removeFromSuperview];
return;
}else {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
CGRect myImageRect = CGRectMake((touchPoint.x -40), (touchPoint.y -45), 80.0f, 90.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"pg6_dog_button.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
[imagesArray addObject:myImage];
NSNumber *arrayCount =[self.view.subviews count];
viewArrayCount.text =[NSString stringWithFormat:@"%d",arrayCount];
imageCount=imageCount++;
imageCounter.text =[NSString stringWithFormat:@"%d",imageCount];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要的是一种将添加的
UIImageView
对象与背景UIImageView
区分开来的方法。我可以想到两种方法来做到这一点。方法 1:为添加的
UIImageView
对象分配一个特殊的标签值每个
UIView
对象都有一个tag
属性,它只是一个可用于标识该视图的整数值。您可以将每个添加的视图的标签值设置为 7,如下所示:然后,要删除添加的视图,您可以逐步遍历所有子视图,仅删除标签值为 7 的子视图:
方法 2:记住背景视图
另一种方法是保留对背景视图的引用,以便您可以将其与添加的视图区分开来。为背景
UIImageView
创建一个IBOutlet
并在 Interface Builder 中以通常的方式分配它。然后,在删除子视图之前,只需确保它不是背景视图即可。What you need is a way of distinguishing the added
UIImageView
objects from the backgroundUIImageView
. There are two ways I can think of to do this.Approach 1: Assign added
UIImageView
objects a special tag valueEach
UIView
object has atag
property which is simply an integer value that can be used to identify that view. You could set the tag value of each added view to 7 like this:Then, to remove the added views, you could step through all of the subviews and only remove the ones with a tag value of 7:
Approach 2: Remember the background view
Another approach is to keep a reference to the background view so you can distinguish it from the added views. Make an
IBOutlet
for the backgroundUIImageView
and assign it the usual way in Interface Builder. Then, before removing a subview, just make sure it's not the background view.仅用一行功能代码即可更快速地编写方法#1:
A more swiftly code for approach #1 in only one functional line of code :