0

I have a:

myViewController - subclass of UIViewController

myFirstView - subclass of UIView

mySecondView - subclass of UIView

Inside myFirstView I added a UIView object called myLittleView as subview to myFirstView's window (UIWindow) and added aUIPanGestureRecognizer` to it so the user can drag the view in the window.

I've also added a CGRect object inside myFirstView called secondViewFrame that's contains the frame of the mySecondView object' frame.

I've added myFirstView and mySecondView object as subview to myViewController's view.

Now I want to check if myLittleView is ontop of mySecondView, I've tried to add this code to the UIPanGestureRecognizer's action but it gave the wrong result:

let pointInViewController = sender.translationInView(self.superview)
if self.secondViewFrame!.contains(pointInViewController) {
   print("Inside")
}

Any idea how can I make it work?

Thanks!

FS.O6
  • 1,394
  • 2
  • 20
  • 42
  • 1
    YOu can always check the subViews which is an array, and check the position of your view. – Teja Nandamuri Aug 31 '16 at 14:24
  • @TejaNandamuri How can I check if the first view in ontop of the second – FS.O6 Aug 31 '16 at 14:25
  • you can give the tag to views, and in the subviews array, get the reference of the two views by the tag, and then check the index position of those two views in the array. The one having the lower index is the front most. – Teja Nandamuri Aug 31 '16 at 14:27
  • Have you tried this: http://stackoverflow.com/a/21433014/6597361 – tech4242 Aug 31 '16 at 15:00
  • I thought `self.secondViewFrame` was a `CGRect`. `CGRect` has no method `pointInside`. If `myFirstView` and `mySecondView` are sibling subviews of your view controller you should consider moving gesture recognizer action to your view controller and using `if CGRectIntersectsRect(myFirstView.frame, mySecondView.frame)` to see if they're overlapping. – beyowulf Aug 31 '16 at 15:00
  • @beyowulf I mean `contains`, not `pointInside` – FS.O6 Aug 31 '16 at 15:02
  • @FS.O6 describe what "gave the wrong result" means. When is `secondViewFrame` set? – beyowulf Aug 31 '16 at 15:37
  • @FS.O6 i have answered , do let me know... – Dravidian Aug 31 '16 at 20:24

2 Answers2

0
- (void)listSubviewsOfView:(UIView *)view {

            // Get the subviews of the view
            NSArray *subviews = [view subviews];

            // Return if there are no subviews
            if ([subviews count] == 0) return; // COUNT CHECK LINE

            for (UIView *subview in subviews) {

                // Do what you want to do with the subview
                NSLog(@"%@", subview);

                // List the subviews of subview
                [self listSubviewsOfView:subview];
            }
        }

You can modified it a little bit to check if a view has a subview with tag.

Update with Tags

When you initialise secondView set a Tag:

 [mySecondView setTag:10];

Then use this method:

- (void)checkIfView:(UIView *)view isParentOfViewWithTag:(NSInteger)tag {

    // Get the subviews of the view
    NSArray *subviews = [view subviews];

    // Return if there are no subviews
    if ([subviews count] == 0) return; // COUNT CHECK LINE

    for (UIView *subview in subviews) {

        if ([subview tag]==tag) {
            //Do something
            NSLog(@"The view is parent of the view with tag 10");
            break;
        }

        [self listSubviewsOfView:subview];
    }
}

On your code:

[self checkIfView:myLittLittleView isParentOfViewWithTag:10];
BlackM
  • 3,927
  • 8
  • 39
  • 69
  • I can't understand how does it relates to check if the view is under the other view – FS.O6 Aug 31 '16 at 15:11
  • I gave you the tools to build your method as you want. I will update the answer with a complete method using Tags. – BlackM Aug 31 '16 at 17:13
0

What you are looking for is a landscape in which your UIView's are in a 3D coordinates, one in front of the other, So i suggest you tackle this with 3D coordinates system

@IBOutlet weak var myFirstView : UIView!

@IBOutlet weak var mySecondView : UIView!

As you have already defined their x and y coordinates by defining their frame,for changing their z-position use:-

mySecondView.layer.zPosition = 0   //Second view on top
myFirstView.layer.zPosition = -1   //First view behind your mySecondView

Whenever you want to change their respective position, just swap .zPosition values for both of them.

to check their relative position:-

  if mySecondView.layer.zPosition == 0 {
      print("secondView is on the top of firstView")
        }else{
      print("firstView is on the top of secondView")
     }
Dravidian
  • 9,945
  • 3
  • 34
  • 74