views:

114

answers:

3

How do I capture touch events such as - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event without subclassing a UIView nor using UIViewControllers.

What happens is that I have a simple UIView created programmatically and I need to detect basic tap events.

+1  A: 

There's just no reason not to. If you subclass and add nothing it's just a UIView called by another name. All you are doing is intercepting those functions that you are interested in. Don't forget you can do [super touchesBegan:touches] inside your subclass' touchesBegan if you don't want to stop responders up the chain from getting those events too.

Adam Eberbach
This good advice, but not an answer the question as stated.
hotpaw2
Looks like he knows the answer - touchesBegan, etc.
Adam Eberbach
+1  A: 

I don't why you don't want to use the normal method of subclassing a UIView to capture touch events, but if you really need to do something weird or sneaky, you can capture all events (including touch events) before they get sent down the view hierarchy by trapping/handling the sendEvent: method at the UIWindow level.

hotpaw2
This is useful tip, but not what i wanted.
samwize
+2  A: 

If you are writing your app for iOS 4, use UIGestureRecognizer. You can then do what you want. Recognize gestures without subclassing.

Otherwise, subclassing is the way to go.

bstahlhood
That's a solution. I can use UIView `addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer`.
samwize
Side note - that was added in 3.2, so you can use them on an iPad as well.
Kendall Helmstetter Gelner