views:

566

answers:

1

I've got a big UIView that responds to touches, and it's covered with lots of little UIViews that respond differently to touches. Is it possible to touch anywhere on screen and slide around, and have each view know if it's being touched?

For example, I put my finger down on the upper left and slide toward the lower right. The touchesBegan/Moved is collected by the baseView. As I pass over itemView1, itemView2, and itemView3, control passes to them. If I lift my finger while over itemView2, it performs itemView2's touchesEnded method. If I lift my finger over none of the items, it performs baseView's touchesEnded.

At the moment, if I touch down on baseView, touchEnded is always baseView and the higher itemViews are ignored.

Any ideas?

A: 

If I understand correctly, the touchesEnded event is detected, but not by the subview that needs to know about it. I think this might work for you:

In a common file, define TOUCHES_ENDED_IN_SUPERVIEW as @"touches ended in superview".

In the touchesEnded method of the containing view that is firing, add

[[NSNotificationCenter defaultCenter] postNotificationName:  TOUCHES_ENDED_IN_SUPERVIEW object: self];

In the touchesBegan of the subviews, add

[[NSNotificationCenter defaultCenter] addObserver: self 
    selector: @selector(touchesEnded:) 
    name: TOUCHES_ENDED_IN_SUPERVIEW 
    object: self.superview];

In the touchesEnded methods of the subviews, use your normal logic for the event, and also add

[[NSNotificationCenter defaultCenter] removeObserver: self name: TOUCHES_ENDED_IN_SUPERVIEW object: self.superview];

Remember to put [[NSNotificationCenter defaultCenter] removeObserver: self] in your dealloc as well, in case it is possible to leave the page without getting the touchesEnded event.

You might want the notification to send its message to a special touchesEndedInSuperview method, which would invoke touchesEnded itself, but that depends on whether you have any special processing to do in that case.

Amagrammer
Thanks Amagrammer, I'll give this a shot. It sounds good to me, but I've since sectioned my code to not involve this for the time being, and I don't want to start opening those gates again. If it works I'll be sure to let you know.
Kevin Beimers