问候所有,
我是一个菜鸟,我一直在努力解决这个问题.
我通过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];
}
Run Code Online (Sandbox Code Playgroud)
}
Jam*_*ton 28
您需要的是一种区分添加的UIImageView对象与背景的方法UIImageView.我有两种方法可以做到这一点.
方法1:为添加的UIImageView对象分配特殊标记值
每个UIView对象都有一个tag属性,它只是一个整数值,可用于标识该视图.您可以将每个添加的视图的标记值设置为7,如下所示:
myImage.tag = 7;
Run Code Online (Sandbox Code Playgroud)
然后,要删除添加的视图,您可以单步执行所有子视图,只删除标记值为7的子视图:
for (UIView *subview in [self.view subviews]) {
if (subview.tag == 7) {
[subview removeFromSuperview];
}
}
Run Code Online (Sandbox Code Playgroud)
方法2:记住背景视图
另一种方法是保留对背景视图的引用,以便将其与添加的视图区分开来.创建一个IBOutletfor background UIImageView并在Interface Builder中以通常的方式指定它.然后,在删除子视图之前,请确保它不是背景视图.
for (UIView *subview in [self.view subviews]) {
if (subview != self.backgroundImageView) {
[subview removeFromSuperview];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
50682 次 |
| 最近记录: |