我使用以下代码将UIImageviews添加到每10秒调用一次的方法中的视图中:
//every 10 seconds
numberofObjects++;
UIImageView *Object = [[UIImageView alloc] initWithFrame:CGRectMake(arc4random()%248+36,arc4random()%496+36,36 ,36)];
Object.tag = [self.view.subviews count]+1;
UIImage *imag = [UIImage imageNamed:@"power-up1.png"];
[Object setImage:imag];
[wallarray addObject:Object];
[self.view insertSubview:Object atIndex:[self.view.subviews count]];
然后我使用这个代码,该代码位于每隔0.03秒调用一次从视图中删除子视图的方法中:
//every 0.03 seconds
for (UIImageView *views in wallarray) {
if (CGRectIntersectsRect(views.frame, yellow.frame) || CGRectIntersectsRect(views.frame, green.frame)) {
wall = YES;
numberofObjects--;
for (UIView *subview in [self.view subviews]) {
if (views.tag == views.tag) {
[subview removeFromSuperview];
break;
}
}
[wallarray removeObjectIdenticalTo:views];
if (!wallarray) {
wallarray = [[NSMutableArray alloc]init];
}
return;
}
}
它可以在95%的时间内工作,但有时它不会从视图中删除。但是,它将从wallarray数组中删除。我添加到视图中的UIImageviews越多,问题似乎越来越严重。如果有人能告诉我一个更好的方法从视图中删除图像视图或告诉我代码有什么问题我会很高兴。