views:

213

answers:

2

I have a UIViewController whose view has a custom subview.

This custom subview needs to track touch events and report swipe gestures.

Currently I put touchesBegan, touchesMoved, touchesEnded and touchesCancelled in the subview class. With some extra logic I am able to get swipe gestures and call my handleRightSwipe and handleLeftSwipe methods. So now when I swipe within the subview it calls its local swipe handling methods. This all works fine.

But what I really need is for the handleRightSwipe and handleLeftSwipe methods to be in the view controller. I could leave them in the subview class but then I'd have to bring in all the logic and data as well and that kind of breaks the MVC idea.

So my question is is there a clean way to handle this? Essentially I want to keep my touch event methods in the subview so that they only trigger for that specific view. But I also want the view controller to be informed when these touch events (or in this case swipe gestures) occur.

Any ideas?

Thanks.

UPDATE:

Using Henrik's suggestion, here's a quick sample of what I did (to save you the reading):

I set my view controller as an observer of notifications (early on).

// NOTIFICATION_LEFT_SWIPE is defined as some unique string elsewhere.
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

// Note that imageView is the instance of my subview that is calling the notification.
// You can set this to nil if you don't want it to be specific.
[nc addObserver:self selector:@selector(handleLeftSwipe) name:@NOTIFICATION_LEFT_SWIPE object:imageView];

Then I implement the handleLeftSwipe method. This will be called when a notification is received.

Now in my subview I send a notification when a swipe gesture is received:

// Note that NOTIFICATION_LEFT_SWIPE is the same one used in the view controller
// I put this in a global header I use.  This is how you keep track of notifications.
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@NOTIFICATION_LEFT_SWIPE object:self]; 

And similarly for the right swipe.

+1  A: 

You could use the Notification Center to send notifications to different (observing) objects.

Henrik P. Hessel
Ah thank you sir! This worked perfectly. I haven't even heard of the notification center before but it does exactly what I needed.
Nebs
A: 

You could also use the gesture-recognizer system, which is available in OS 3.2 and later; gesture recognizers let you decouple touch handling from your view classes, and are designed for pretty much exactly the kind of thing you're doing here. In this case, you'd create an instance of UIPanGestureRecognizer, add your controller as a target of the recognizer (using the usual -addTarget:action: syntax), then add the recognizer to your view with -addGestureRecognizer:. Your controller, then, would get a series of action messages called on it as the user swiped across the view, and could check on the movement by calling the recognizer's -translationInView: method.

Noah Witherspoon