Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup UIView touch handler without subclassing

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.

like image 503
samwize Avatar asked Dec 21 '25 14:12

samwize


2 Answers

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.

like image 125
bstahlhood Avatar answered Dec 23 '25 04:12

bstahlhood


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.