【问题标题】:iphone programming: collision detection of a UIImageView and an animationiphone编程:UIImageView和动画的碰撞检测
【发布时间】:2012-10-12 05:03:19
【问题描述】:

我在 viewDidAppear 中有一个动画,如下所示

-(void)viewDidAppear:(BOOL)animated
{
    for (int i = 0; i < 100; i++) {

        p = arc4random_uniform(320)%4+1; //global integer

        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];
                         }];
    }
}

它只是从屏幕顶部创建小方块并移动到底部。 我还有一个 UIImageView,它由 x 轴上的加速度计控制。目标不是触摸动画对象。就像一个简单的赛车游戏。但是我不知道如何检测 imageView 和动画之间的碰撞?

【问题讨论】:

    标签: iphone objective-c ios animation collision-detection


    【解决方案1】:

    首先,您需要将所有动画视图对象保存在一个数组中,以检查与图像视图的冲突。所以在你的 .h 文件中创建一个 NSArray 来保存动画视图

    NSMutableArray animatedViews;
    

    然后在 viewDidLoad 中初始化它

    animatedViews = [[NSMutableArray alloc] init];
    

    然后更改代码 viewWillAppear 并添加一行

    [self.view addSubview:animatedView];
    [animatedViews addObject:animatedView];
    

    然后创建一个检查碰撞的函数,它需要一个预定计时器的参数

    -(void) checkCollision: (NSTimer *) theTimer{
        for(int i = 0; i < [animatedViews count]; i++) {
            UIView *theView = [animatedViews objectAtIndex:index];
            if (CGRectIntersectsRect(theView.frame, yourImageView.frame) {
               // the image collided
               // stop the timer and do your work here
            }
        }
    }
    

    然后在 viewWillAppear 中添加这一行来安排计时器每半秒调用一次以调用函数来检查碰撞

    [NSTimer scheduledTimerWithTimeInterval: 0.5  
                                 target: self  
                               selector: @selector(checkCollision:)  
                               userInfo: nil  
                                repeats: YES];
    

    【讨论】:

    • 感谢您的解决方案。我完全按照你说的做了。我认为这真的很有意义。但是,什么都没有改变。我仍然无法捕捉到碰撞。
    • 哎呀,我把时间间隔写错了5.0(五)秒,你是不是把它改成0.5半秒后运行。
    • 您需要检查presentationLayer,因为它是在动画期间更新的。检查实际的视图框架根本没有帮助,因此这个答案是错误的。
    • @Till 你能建议正确的方法吗?
    【解决方案2】:

    问题出在这个测试中:if (CGRectIntersectsRect(theView.frame, yourImageView.frame) 你必须检查视图的presentationLayer:

    CGRect movingFrame = [[theImage.layer presentationLayer] frame];
        if (CGRectIntersectsRect(movingFrame, panier.frame)) {           
            // the image collided            
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 2017-08-28
      • 2021-04-12
      相关资源
      最近更新 更多