简体   繁体   English

iOS CGRectIntersectsRect与UIImageView的多个实例?

[英]iOS CGRectIntersectsRect with multiple instances of UIImageView?

I have a layout for an application that needs to detect when an image collides with another image. 我有一个应用程序的布局,该布局需要检测某个图像何时与另一个图像碰撞。

Here, the user creates multiple 'balls', the same instance of a UIImageView named 'imgView', on the screen by tapping the location they desire: 在这里,用户通过点击所需的位置在屏幕上创建多个“球”,即UIImageView名为“ imgView”的UIInstance的相同实例:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *myTouch = [[event allTouches] anyObject];
    imgView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, 40, 40)];
    imgView.image = [UIImage imageNamed:@"ball.png"];
    [self.view addSubview:imgView];
    imgView.center = [myTouch locationInView:self.view];

}

(imgView is declared as a UIImageView in the header) : (imgView在标头中声明为UIImageView):

    UIImageView *imgView;

Now, I also have an image called 'staff'. 现在,我还有一张名为“ staff”的图片。 It is a long bar that pans horizontally across the screen. 它是一个长条,可在屏幕上水平平移。 I want the image 'staff' to detect EVERY collision it has with the variable 'imgView', or the balls the user has placed on the screen. 我希望图像“ staff”检测到它与变量“ imgView”或用户放置在屏幕上的球的所有碰撞。 Therefore, the user could tap 10 different places on the screen and and 'staff' should be able to catch every single one. 因此,用户可以在屏幕上点击10个不同的位置,并且“工作人员”应该能够捕捉到每个单独的位置。

I use this CGRectIntersectsRect code thats activated by an NSTimer: 我使用由NSTimer激活的CGRectIntersectsRect代码:

-(void)checkCollision {
    if( CGRectIntersectsRect(staff.frame,imgView.frame)) {
    [self playSound];
    }
}

However, the intersection is only detected with the LAST instance, or 'ball' the user created. 但是,仅使用LAST实例或用户创建的“球”检测到相交。 The staff reacts to that one, but pans over the rest. 工作人员对此做出反应,但平移其余工作。 Any help to fix my code to detect all instances would be greatly appreciated. 修复我的代码以检测所有实例的任何帮助将不胜感激。

Each time you create a new ball, you are overriding your imgView instance variable. 每次创建一个新球时,您都将覆盖imgView实例变量。 Thus, your checkCollision method only sees the latest value of imgView , namely the last ball created. 因此,您的checkCollision方法只能看到的最新值imgView ,即创建的最后一个球。

Instead, you could keep track of every ball on screen in an NSArray , then check for collisions against every element in that array. 相反,您可以跟踪NSArray屏幕上的每个球,然后检查与该阵列中每个元素的碰撞。 To do that, replace your imgView instance variable with, say: 为此,将您的imgView实例变量替换为:

NSMutableArray *imgViews

Then, sometime early on, say in viewDidLoad initialize the array: 然后,早些时候说在viewDidLoad初始化数组:

 imgViews = [[NSMutableArray alloc] init]

In -touchesEnded:withEvent: add the new UIImageView to the array: -touchesEnded:withEvent:将新的UIImageView添加到数组中:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

     UITouch *myTouch = [[event allTouches] anyObject];
     UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, 40, 40)];
     imgView.image = [UIImage imageNamed:@"ball.png"];
     [self.view addSubview:imgView];
     imgView.center = [myTouch locationInView:self.view];
     [imgViews addObject:imgView]
}

And finally, in checkCollision iterate through your array and perform your check on every element 最后,在checkCollision遍历数组并对每个元素执行检查

 - (void)checkCollision {
      for (UIImageView *imgView in imgViews) {
           if( CGRectIntersectsRect(staff.frame,imgView.frame)) {
                 [self playSound];
      }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM