mlevi mlevi -4 years ago 92
Swift Question

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. 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.

Answer Source

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
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download