Detecting if UIView is intersecting other UIViews

前端 未结 4 1006
谎友^
谎友^ 2020-12-03 09:35

I have a bunch of UIViews on the screen. I would like to know what\'s the best to way to check if a particular view (which I have reference to) is intersection ANY other vie

4条回答
  •  情话喂你
    2020-12-03 09:55

    To Achieve the same thing in swift as per the accepted answer then here is the function. Readymade Code. Just copy and use it as per the steps. By the way I am using Xcode 7.2 with Swift 2.1.1.

    func checkViewIsInterSecting(viewToCheck: UIView) -> Bool{
        let allSubViews = self.view!.subviews //Creating an array of all the subviews present in the superview.
        for viewS in allSubViews{ //Running the loop through the subviews array
            if (!(viewToCheck .isEqual(viewS))){ //Checking the view is equal to view to check or not
                if(CGRectIntersectsRect(viewToCheck.frame, viewS.frame)){ //Checking the view is intersecting with other or not
                    return true //If intersected then return true
                }
            }  
        }
        return false //If not intersected then return false
    }
    

    Now call this function as per the following -

    let viewInterSected = self.checkViewIsInterSecting(newTwoPersonTable) //It will give the bool value as true/false. Now use this as per your need
    

    Thanks.

    Hope this helped.

提交回复
热议问题