Objective-C – Options for handling touch events in Cocoa-Touch
So I believe there are numerous options for handling touch events when programming for the iDevices. A few of the options that I have come a cr开发者_StackOverflow中文版oss are UITapGestureRecognizer
and of course using UIButton
s. Are there anymore options? What options are most suitable to use? Perhaps someone has a link to a guide or a tutorial that sums this up?
Cheers,
Peter
1) Target-action: UIControl
(UIButton
is a subclass of that) provides some built-in touch handling by adding a target and action for certain types of touch events. Example:
[myButton addTarget:self
action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
2) Overriding the UIView
methods touchesBegan:withEvent:
, touchesMoved:withEvent:
, touchesEnded:withEvent:
and touchesCancelled:withEvent:
– Provides very fine-grained control but can be difficult to use for complex multitouch handling.
3) Gesture recognizers (since iOS 3.2): Recognition of multitouch (or single touch) gestures that are usually comprised of multiple touch events. The built-in gesture recognizers provide support for recognizing taps, pinches, rotation gestures, swipes, panning and long presses. It's also possible to create custom gesture recognizers for more complex gestures.
All the gesture recognizer subclasses are customizable to a certain degree, e.g. you can specify a minimum number of taps and touches for a UITapGestureRecognizer
.
Generally, gesture recognizers can both provide discrete events (like a tap) and continuous events (like a rotation that changes its angle over time).
The best resource by far is the WWDC 2011 Video on Multi-Touch (requires a developer account):
http://developer.apple.com/itunes/?destination=adc.apple.com.8270634034.08270634040.8367260921?i=1527940296
This goes over using both gesture recognizers as well as custom touch handling.
I'd use the UIGestureRecognizers for specific gestures (pan, pinch etc) or the touch handling methods which are inherited from UIResponder class (in UIViewController for instance)....
– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:
There are a LOT of resources on handling touches in Cocoa-touch. Just google it or even search here on this site for specifics.
精彩评论