下面的代码只是从屏幕顶部创建小方块并移动到底部。我还有一个 UIImageView(player),它由 x 轴上的加速度计控制。目标不是触摸动画对象。就像一个简单的赛车游戏。虽然我没有错误,但我无法检测到碰撞。我在我的代码中找不到错误。可能是什么问题?
-(void)printOut:(NSTimer*)timer1;
{
for (int i = 0; i < 100; i++) {
p = arc4random_uniform(320)%4+1;
CGRect startFrame = CGRectMake(p*50, -50, 50, 50);
CGRect endFrame = CGRectMake(p*50, CGRectGetHeight(self.view.bounds) + 50,
50,
50);
animatedView = [[UIView alloc] initWithFrame:startFrame];
animatedView.backgroundColor = [UIColor redColor];
[self.view addSubview:animatedView];
[UIView animateWithDuration:2.f
delay:i * 0.5f
options:UIViewAnimationCurveLinear
animations:^{animatedView.frame = endFrame;}
completion:^(BOOL finished) {[animatedView removeFromSuperview];}];
CGRect movingFrame = [[animatedView.layer presentationLayer] frame];
if(CGRectIntersectsRect(player.frame, movingFrame)){
[timer invalidate];
NSLog(@"asdad");
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"You Lost" message:[NSString stringWithFormat:@"You were hit!"] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alert show];
}
}
[timer1 invalidate];
}