UIPanGestureRecognizer 碰撞

发布于 2024-12-19 09:15:49 字数 570 浏览 0 评论 0原文

我有 6 个 UIImageView,每个都连接到 UIPanGestureRecognizer,并且它们都连接到相同的方法。方法是:

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

我正在遵循 Ray Wenderlich 的关于使用 GestureRecognizers 的教程。所以,我想知道如何检测碰撞,以便当一个图像与另一图像碰撞时,运行一些代码。每个图像的代码都不同。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪意 2024-12-26 09:15:50

如果您想使用识别器移动图像,也许您应该将识别器附加到您的视图中。

属于此,最快的方法是(在更改 UIImageView 的框架的方法中)

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

_selectedImageView 是您正在移动的图像,_imageArray 是包含所有 UIImageView 的数组(在您的例子中是 6)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文