本文介绍了快速检测两个UIView的碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的ViewController上有两个UIViews。我将panGesture添加到第一个视图中,当我开始移动此视图时,第二个视图将移向第一个视图。我希望在这两个视图发生碰撞时检测到一个事件。这是我的代码。
I have two UIViews on my ViewController. I added panGesture to first view and when i start moving this view the second view will move towards first view. I want to detect an event when these two views collides. Here is my code.
@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
}
这篇关于快速检测两个UIView的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!