检查是否在其他视图中查看?

时间:2015-02-05 13:09:10

标签: ios swift uiimageview uipangesturerecognizer

我正在使用 UIPanGestureRecognizer 来移动UIImageView。现在,我需要检查用户是否在其他矢量PDF UIImageView中移动了我的对象。

例如,我们有红条,我们正在移动,我们需要在蓝条内移动红条(蓝条是矢量PDF)。我们怎么检查呢?

示例图片:http://i.imgur.com/PKXKHBi.png

示例代码:

@IBAction func moveRedBox(recognizer: UIPanGestureRecognizer) {
    let translation = recognizer.translationInView(self.view)
    recognizer.view!.center = CGPoint(x:recognizer.view!.center.x + translation.x,
                                      y:recognizer.view!.center.y + translation.y)
    recognizer.setTranslation(CGPointZero, inView: self.view)

    if recognizer.state == UIGestureRecognizerState.Ended {
        // Here we will check that redBox inside blueBox
    }
}

3 个答案:

答案 0 :(得分:2)

要检查两个视图是否重叠,您可以使用CGRectIntersectsRect功能。

更新:刚刚发现Swift中的CGRect上还有一个交叉方法

let doRectsIntersect = rect1.intersects(rect2) 

答案 1 :(得分:1)

检查一下。这对我有所帮助:

if view1.frame.contains(view2.center) {
    // do your thing
  }

完整示例:

@IBAction func moveRedBox(recognizer: UIPanGestureRecognizer) {

  ...

  switch recognizer.state {

  case .Ended:

    if blueBox.frame.contains(recognizer.view!.center) {
      print("Yep")
    }
  }

}

答案 2 :(得分:0)

检查UIImageView的位置

image.frame.origin.x >= view.frame.origin.x
image.frame.origin.y >= view.frame.origin.y

在移动完成后输入此代码