UIGestureRecognizer using MonoTouchPosted in iPhone MonoTouch development on February 23, 2010 by Pavel Sich When implementing touch and gesture based interaction in your application, for example the swipe effect, you had to handle the TouchesMoved, TouchesBegan, TouchesEnded and TouchesCancelled events and handle the location of the finger and last location and so on, based on the quality of the algorithm it worked or not. With the new SDK and with the great support from MonoTouch team, its current version of MonoTouch implements the UIGestureRecognizer and UIGestureRecognizerDelegate and related classes - UITapGestureRecognizer, UISwipeGestureRecognizer, UIRotationGestureRecognizer, UIPinchGestureRecognizer, UILongPressGestureRecognizer, UIPanGestureRecognizer. Lets have a look and few simple steps to take advantage of these common gesture implementations on the iPhone/iPad. For the demonstration we will use the swipe gesture. First we need to have a view controller and define a Selector for the gesture recognizer. More on Selectors check the iPhone SDK, in general it is a special form of message. In MonoTouch Selectors can be consumed (fired from MonoTouch code) or exposed, being available to SDK functions and classes to be called from within. Lets have a look at the typical implementation:
Please note that the Selector is defined as a static access property returning the identification of the selector via its name (HandleSwipe). Next we need to define the gesture recognizer and its delegate and assign it to the view. We do this in the ViewDidLoad event of the controller implementation class.
Then we need our delegate class for the swipe recognizer:
And last and finally our handler for the swipe event:
Well, that is all and we are done. Happy coding!
|
