ios - 快速检测两个 UIView 的碰撞

标签 ios collision-detection uipangesturerecognizer

我的 ViewController 上有两个 UIView。我将 panGesture 添加到第一个 View ,当我开始移动此 View 时,第二个 View 将移向第一个 View 。当这两个 View 发生冲突时,我想检测一个事件。这是我的代码。

@IBAction func dragFirstView(sender: UIPanGestureRecognizer) {

        let translation = sender.translationInView(self.view)

        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            UIView.animateWithDuration(2.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
                self.secondView.frame = CGRectMake(sender.view!.center.x + translation.x, sender.view!.center.y + translation.y, self.secondView.frame.size.width, self.secondView.frame.size.height)
                }, completion: nil)
        }

        sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y + translation.y)
        sender.setTranslation(CGPointZero, inView: self.view)
    }

最佳答案

关于

if (CGRectIntersectsRect(secondView.frame, sender.frame)) {
        // Do something
    }

CGRectIntersectsRect(::) : Returns whether two rectangles intersect.

关于ios - 快速检测两个 UIView 的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31587208/

相关文章:

ios - 使用用户 iOS 货币设置

android - 每个线段的不同颜色?

c# - 在运行时更新网格对撞机的替代方案?

java - 如何检查圆是否位于凸多边形内部

swift - 以编程方式添加 View 和手势 - "unrecognized selector sent to instance"

ios - 如果平移,UIButton 框架不会改变

objective-c - 如何使用捏缩放(UIPinchGestureRecognizer)更改UIView的宽度

objective-c - iOS 中与 Cocoa 的 `NSSwitchButton` 复选框最接近的模拟是什么?

javascript - 使用 HTML5 和 JavaScript 进行碰撞检测的最佳方法?

swift - ARSCNView 中的平滑对象旋转