我有 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 的教程。所以,我想知道如何检测碰撞,以便当一个图像与另一个图像碰撞时,运行一些代码。每张图片的代码都不同。
谢谢
最佳答案
如果你想用识别器移动图像,也许你应该将识别器附加到你的 View 。
属于这个,最快的方法是(在你的 UIImageView 改变框架的方法)
for (UIImageView *iv in _imageArray){
if (CGRectIntersectsRect(iv.frame, _selectedImageView.frame)) {
NSLog(@"Collision");
}
}
_selectedImageView
是您正在移动的图像,_imageArray
是一个包含所有 UIImageView 的数组(在您的例子中是 6)。
关于iphone - UIPanGestureRecognizer 碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8369565/