Ios 如何检查UIView是否超出其superview界限

Ios 如何检查UIView是否超出其superview界限,ios,objective-c,iphone,swift,uiview,Ios,Objective C,Iphone,Swift,Uiview,我有一个带有平移手势和连接到它的UIPushBehavior的视图,我想知道是否有可能在视图超出SuperView边界时进行检查。基本上,用户抛出视图,我想在视图离开屏幕时运行一些动画。我想不出怎么做。谢谢。如果您想检查它是否完全超出了superview的范围,您可以这样做 if (!CGRectContainsRect(view.superview.bounds, view.frame)) { //view is completely out of bounds of its supe

我有一个带有平移手势和连接到它的UIPushBehavior的视图,我想知道是否有可能在视图超出SuperView边界时进行检查。基本上,用户抛出视图,我想在视图离开屏幕时运行一些动画。我想不出怎么做。谢谢。

如果您想检查它是否完全超出了superview的范围,您可以这样做

if (!CGRectContainsRect(view.superview.bounds, view.frame))
{
    //view is completely out of bounds of its super view.
}
如果你想检查是否有一部分超出了范围,你可以这样做

if (!CGRectEqualToRect(CGRectIntersection(view.superview.bounds, view.frame), view.frame))
{
   //view is partially out of bounds
}
编辑
正如所指出的,这个答案并不完全正确,请参考投票结果更高的答案

在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
    }

不幸的是,Philipp关于部分越界检查的回答在这方面并不完全正确:
v1.bounds.intersection(v2.frame).width>0)和&(v1.bounds.intersection(v2.frame).height>0

交集大小可以大于零,但视图仍位于superview边界内

另外,由于CGFloat的准确性,我不能安全地使用
equal(to:CGRect)

以下是更正版本:

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
}
func outOfSuperviewBounds()->Bool{
guard let superview=self.superview-else{
返回真值
}
让intersectedFrame=superview.bounds.intersection(self.frame)
设isInBounds=fabs(intersectedFrame.origin.x-self.frame.origin.x)<1&&
fabs(intersectedFrame.origin.y-self.frame.origin.y)<1&&
晶圆厂(intersectedFrame.size.width-self.frame.size.width)<1&&
晶圆厂(相交框架尺寸高度-自框架尺寸高度)<1
回来!伊辛博兹
}
Swift 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
    }
func-isView(\uinnerview:UIView,outOfViewFrame-outerViewFrame:CGRect)->Bool{
让intersectedFrame=outerViewFrame.intersection(innerView.frame)
设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
回来!伊辛博兹
}

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
}

刚刚尝试过这个,它似乎在视图仍在superview的范围内时也会被调用:/If it help我所做的事情基于本教程:基本上我想在视图离开屏幕后做一些事情抱歉,我误解了你的问题。你需要将该代码放在手势识别器的操作中。你有没有此时,即使视图仍在边界内,也会调用我放在中的代码