Ahan Malhotra
Ahan Malhotra

Reputation: 185

UIPanGestureRecognizer Collision

I have 6 UIImageViews each connected to UIPanGestureRecognizer and they are all connected to the same method. The method is:

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {

CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
                                     recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}

I am following Ray Wenderlich's tutorial on using GestureRecognizers. So, I was wondering how to detect collisions so that when one image collides with another image, some code is run. The code is different for each image.

Thanks

Upvotes: 0

Views: 662

Answers (1)

Fry
Fry

Reputation: 6275

If you want move the image with the recognizer maybe you should attach the recognizer to your view.

Belonging to this, the fastest way to do this, is (in the method that change the frame at your UIImageView)

for (UIImageView *iv in _imageArray){
   if (CGRectIntersectsRect(iv.frame, _selectedImageView.frame)) {
      NSLog(@"Collision");
   }
}

_selectedImageView is the image that your are moving and _imageArray is an array that contains all your UIImageView (in your case are 6).

Upvotes: 1

Related Questions