Search code examples
iosswiftuiviewswift2cgrect

How i get subView using a Rect in Swift 2.0


There is a Main View with subview. I need to get these subviews.

For example, I created a view:

enter image description here

With the Swift Code:

import UIKit

class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        // Check if has view in rect area

        let rect1 = CGRect(x: 0, y: 0, width: 100, height: 100)

        if CGRectContainsPoint(rect1, rect1.origin) { // return true

            print("TRUE")

        } else {

            print("FALSE")

        }
    }
}

I need to get the view you are "touching" the Rect in size or origin.

In the above example, I created three subviews with different colors between them.

I need to get the view and make a print() with the backgroundColor.

Can someone help me?


Solution

  • This func fix the problem:

    func intersectingViewWithView(panView: UIView) -> UIView? {
        for view in self.view.subviews {
            if view != panView {
                if CGRectIntersectsRect(panView.frame, view.frame) {
                    return view
                }
            }
        }
        return nil
    }