views:

40

answers:

3

Hi

Is there a way to know when a view is viewable to user?

For example: if I have 2 views, and one of them is "hiding" the other-

how can I tell that the "hidden" view is currently not visible on screen,

and how can I check to see when it becomes visible again?

Thanks.

A: 

UIView's

– viewWillAppear:

Notifies the view controller that its view is about to be become visible.It means every time your view is about to appear this method will be called automatically

same way

- (void)viewWillDisappear:(BOOL)animated

delegate method will automatically called when the view is about to be dismissed, covered, or otherwise hidden from view.(just before it hides)

mihirpmehta
If it is partially hidden by another view- will it call- (void)viewWillDisappear:(BOOL)animated?
Shay
IT should be... I am not sure... you can put NSLog in the viewWillDisappear method and verify the same...
mihirpmehta
Thanks, but it seems that viewWillDisapear only gets called when its view is removed - not when it is hidden by another view.
Shay
you can get the same event by implementing viewWillAppear method of that view which is going to overlap your current view... Are you getting what i am trying to say....?
mihirpmehta
Yes Thanks, but it is not answering my specific needs.
Shay
what is your exact need...? i mean what's your exact goal...?
mihirpmehta
A: 

I will assume you mean two views in the same window. If the views draw outside their frames, which any view may do when clipsToBounds is NO, then you will have to get the true bounding frame of the view contents.

If you have two views with the same parent view and you want to see if they intersect you can use the following:

CGRect frame1 = [view1 frame];
CGRect frame2 = [view3 frame];
CGRectIntersectsRect( frame1 , frame2 );

If the two views do not have the same parent, then you would have to find a common parent of both views and use:

CGRect frame1 = [parent convertRect:[view1 frame] fromView:view1];
CGRect frame2 = [parent convertRect:[view2 frame] fromView:view2];

If you want to know if the views overlap completely, instead of if they overlap a little then use this instead of CGRectIntersectsRect:

CGRectContainsRect( frame1 , frame2 ) || CGRectContainsRect( frame2 , frame1 )

If the two views are not opaque, then even though their frames intersect the contents of the views may not. Figuring that out is totally dependent on the specific contents of the views.

Once you have figured out that the views do overlap, you can figure out which one is on top by examining [parent subviews] and seeing which view has a higher index. If either view is not a direct subview of the parent, you can walk the subviews and use isDescendantOfView to find the order.

To find out if a view is hidden in general, you would compare it to every other view that has a higher z order. The z order of a view is the same as the index in the subviews array, so the subview at index 2 has a higher z order than the subview at index 1. Start in the parent of the view then ascend the view hierarchy.

drawnonward
Thanks- I really understood your answer only now, after I thought of doing it the same way, But still your answer is now saving me a lot of coding / thinking time.Thanks.
Shay
A: 

I thought of a way and if it will work I'll comment it-

By getting all the views "above" me in the views hierarchy,

and checking their alphas and rects to see if they are hiding my view.

Hope it will work

Shay