我正在尝试检测每秒使用NSTimer创建的下降/动画UIImageView之间的冲突。这是代码:
-(void)newobject{
int randomx = 76+ arc4random() %(290);
int randomDuration = 2+ arc4random() %(6);
int randomImage = 1+ arc4random() %(3) ;
NewObject = [[UIImageView alloc] initWithFrame:CGRectMake(randomx,0,36 ,36)];
UIImage *imag = [UIImage imageNamed:@"ball.png"];
[NewObject setImage:imag];
[self.view addSubview:NewObject];
numberofObjects += 1;
NewObject.tag = numberofObjects;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:randomDuration];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDone:finished:context:)];
NewObject.frame = CGRectMake(randomx, 300,NewObject.frame.size.width,NewObject.frame.size.height);
[UIView commitAnimations];
}
该代码创建了一个名为NewObject的新UIImageView,并将其设置为屏幕下方的动画。为了检测碰撞,我有另一个每1.0 / 60调用的NSTimer。这是代码:
-(void)Collison{
for (int i = 1; i < numberofObjects; i++) {
UIImageView *image = NewObject;
[image viewWithTag:i];
CALayer *layer = image.layer.presentationLayer;
CGRect NewObjectFrame = layer.frame;
CALayer *layer2 = Sprint.layer.presentationLayer;
CGRect SprintFrame = layer2.frame;
if (CGRectIntersectsRect(NewObjectFrame, SprintFrame)) {
[self StopTimers];
NSLog(@"hello");
}
}
}
问题是控制台说“它有效”,但是UIImage没有碰撞,我可以将角色与图像碰撞,但控制台什么也没说,同时如果我试图用UIImageView碰撞角色有用。我想说的是它不可靠。有人可以帮忙吗?
更新:我编辑了代码,但现在它检测到添加的最后一个uiimageview。
答案 0 :(得分:0)