문제

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

도움이 되었습니까?

해결책

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).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top