简体   繁体   English

如何检查 UIView 是否超出其超视图范围

[英]How to check if a UIView is out of its superview bounds

I have a view with a pan gesture and a UIPushBehavior hooked up to it an wanted to know if its possible to check when the view is out the superviews bounds.我有一个带有平移手势的视图和一个连接到它的 UIPushBehavior,想知道是否可以检查视图何时超出超级视图边界。 Basically the user tosses the view and I want to run some animation when the view is out of the screen.基本上用户抛出视图,我想在视图离开屏幕时运行一些动画。 Couldn't figure out how to do this.无法弄清楚如何做到这一点。 Thanks.谢谢。

If you want to check if it is entirely out of it's superview bounds you can do this如果你想检查它是否完全超出了它的超视图范围,你可以这样做

if (!CGRectContainsRect(view.superview.bounds, view.frame))
{
    //view is completely out of bounds of its super view.
}

If you want to check if just part of it is out of bounds you can do如果你想检查它的一部分是否越界,你可以这样做

if (!CGRectEqualToRect(CGRectIntersection(view.superview.bounds, view.frame), view.frame))
{
   //view is partially out of bounds
}

Unfortunately, Philipp's answer on partially out of bounds check is not fully correct in this line: v1.bounds.intersection(v2.frame).width > 0) && (v1.bounds.intersection(v2.frame).height > 0不幸的是,菲利普对部分越界检查的回答在这一行中并不完全正确: v1.bounds.intersection(v2.frame).width > 0) && (v1.bounds.intersection(v2.frame).height > 0

Intersection size can be bigger than zero and still the view would lie inside the superview bounds.交叉点大小可以大于零,并且视图仍然位于超视图边界内。

Also it turned out that I can't use equal(to: CGRect) safely because of CGFloat accuracy.事实证明,由于 CGFloat 的准确性,我不能安全地使用equal(to: CGRect)

Here is corrected version:这是更正的版本:

func outOfSuperviewBounds() -> Bool {
  guard let superview = self.superview else {
    return true
  }
  let intersectedFrame = superview.bounds.intersection(self.frame)
  let isInBounds = fabs(intersectedFrame.origin.x - self.frame.origin.x) < 1 &&
                   fabs(intersectedFrame.origin.y - self.frame.origin.y) < 1 &&
                   fabs(intersectedFrame.size.width - self.frame.size.width) < 1 &&
                   fabs(intersectedFrame.size.height - self.frame.size.height) < 1
  return !isInBounds
}

EDIT编辑
As pointed out, this answer is not entirely correct, please refer to the more upvoted one.正如所指出的,这个答案并不完全正确,请参阅更多赞成的答案。

In Swift 3:在 Swift 3 中:

    let v1 = UIView()
    v1.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
    v1.backgroundColor = UIColor.red
    view.addSubview(v1)

    let v2 = UIView()
    v2.frame = CGRect(x: 100, y: 100, width: 200, height: 200)
    v2.backgroundColor = UIColor.blue
    view.addSubview(v2)

    if (v1.bounds.contains(v2.frame))
    {
        //view is completely inside super view.
    }
    //this should be an or test
    if (v1.bounds.intersection(v2.frame).width > 0) || (v1.bounds.intersection(v2.frame).height > 0)
    {
        //view is partially out of bounds
    }

Swift 5斯威夫特 5

func isView(_ innerView: UIView, outOfViewFrame outerViewFrame: CGRect) -> Bool {
        let intersectedFrame = outerViewFrame.intersection(innerView.frame)
        let isInBounds = abs(intersectedFrame.origin.x - innerView.frame.origin.x) < 1 &&
            abs(intersectedFrame.origin.y - innerView.frame.origin.y) < 1 &&
            abs(intersectedFrame.size.width - innerView.frame.size.width) < 1 &&
            abs(intersectedFrame.size.height - innerView.frame.size.height) < 1
        return !isInBounds
    }

Swift 5 version on accepted answer:接受答案的 Swift 5 版本:

// Full
if subview.superview!.bounds.contains(subview.frame) { 
  // Do something 
}

// Partial
let intersection = subview.superview!.bounds.intersection(subview.frame)
if !intersection.equalTo(subview.frame) { 
  // Do something
}

if you want to check if part of the subview is out of bounds of its super view, try this:如果要检查子视图的一部分是否超出其超级视图的范围,请尝试以下操作:

in case of x axis在 x 轴的情况下

if subview.frame.maxX >= superview.bounds.maxX {
    your code 
}
if subview.frame.minX <= superview.bounds.minX {
    your code
}

you can try the same for y axis, replace minX and maxX to minY and maxY您可以对 y 轴尝试相同的操作,将 minX 和 maxX 替换为 minY 和 maxY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM