大家好,
我是一个菜鸟,几天来我一直在努力解决这个问题。
我正在通过 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];
}
}