Draw beautiful diagram effortlessly on ipad. Click here to know more..

Saturday, February 2, 2013

Touch gestures in iOS (iPhone, iPad) sdk

iOS sdk provides simple and powerful touch gesture API. With few lines of code, you can make your app touch/multitouch aware.
In this blog post, I am posting code snippet for using touch gesture in iOS app. The code snippet is taken from a drawing app: Lekh Diagram
The Lekh Diagram heavily uses touch gestures. Here are touch gestures used by Lekh Diagram

  • pan
  • two finger drag
  • tap
  • double tap
  • long press
  • pinch
  • rotate
All these gestures are used in the app on the drawing canvas. The Lekh Diagram registers and handles all multitouch events except "two finger drag". The two finger drag is handled by UIScrollView. 

Here is code snippet for registering gesture events:

The Lekh Diagram has drawing canvas which derives from UIView as
@interface DrawingView : UIView
@end

In the constructer of this class, following code registers for events.

// *******************************************************************
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc
                                          initWithTarget:self action:@selector(handlePanGesture:)];
    panGesture.minimumNumberOfTouches = 1;
    panGesture.maximumNumberOfTouches = 1;
    [self addGestureRecognizer:panGesture];
    

// ****************************************************************
    UITapGestureRecognizer *doubleTap = 
    [[UITapGestureRecognizer alloc]
     initWithTarget:self action:@selector(handleDoubleTapGesture:)];
    [doubleTap setNumberOfTapsRequired:2];
    [self addGestureRecognizer:doubleTap];
    

// ****************************************************************
    UITapGestureRecognizer *tapGest = 
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    [tapGest requireGestureRecognizerToFail:doubleTap];
    [self addGestureRecognizer:tapGest];
    

// *******************************************************************
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    longPressGesture.minimumPressDuration = 1;
    [self addGestureRecognizer:longPressGesture];
    

// *********************************************************************
    UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleZoom:)];
[self addGestureRecognizer:pinchRecognizer];
    
// *******************************************************************
    UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];
[self addGestureRecognizer:rotationRecognizer];


Here are event handler functions

-(IBAction)handleLongPress:(UILongPressGestureRecognizer*)sender{  
    if(UIGestureRecognizerStateBegan == sender.state) {
     // long press handler code
   }
}

-(IBAction)handleRotate:(UIRotationGestureRecognizer*)sender{
  if([sender state] == UIGestureRecognizerStateEnded) {
     // handler code for rotation end

   }

    else if([sender state] == UIGestureRecognizerStateBegan){

       // handler code for rotation start
    }
    else{
        double rotation = [sender rotation]; // amount of rotation
        // handler code for rotation in progress
    }

}

-(IBAction)handleZoom:(UIPinchGestureRecognizer*)sender{
    if([sender state] == UIGestureRecognizerStateEnded) {
        // handler code for pinch end
    }
    else if([sender state] == UIGestureRecognizerStateBegan){
      // handler code for pinch start
    }
    else{
        CGFloat scale = [(UIPinchGestureRecognizer*)sender scale]; // amount of pinch
       // handler code for pinch progress
    }

}

-(IBAction)handlePanGesture:(UIPanGestureRecognizer *)sender{
     CGPoint location = [sender locationInView:self];  // current touch position
      if(sender.state == UIGestureRecognizerStateEnded){
        // handler code for panning end
      }
      else if(sender.state == UIGestureRecognizerStateBegan){
         // handler code for panning starts
      }
      else {
         // handler code for panning in progress
      }

}

- (IBAction)handleTapGesture:(UIGestureRecognizer *)sender {
    CGPoint location = [sender locationInView:self]; // tap location
   // handler code for tap
}

- (IBAction)handleDoubleTapGesture:(UIGestureRecognizer *)sender {
    CGPoint location = [sender locationInView:self]; // double tap location
    // handler code for double tap
}




Here is demo of Lekh Diagram, you can see touch gesture in action in this video




11 comments:

  1. Thankful such an awesome sum for sharing this glorious information! I am envisioning see more posts by you. I like visiting you site since I always come across interesting articles like this one. Great Job, I greatly appreciate that. Do keep sharing! Your information is good and friendly this article very helpful for me. Thanks for the post. After seeing the following post, I totally inspire from you and your blogs also respectively. It was nice articles and I also very enjoyed after read this. Thanks a lot sir. Visit here also: Best essay writing service

    ReplyDelete
  2. I actually never even though about getting deeper into my iPad settings to get some extra features working properly. I usually use my iPad for music, books and essay paper writing services so I never even bother. Thank you for sharing.

    ReplyDelete
  3. I discovered your this post while hunting down data about web journal related exploration ... It's a decent post .. continue posting and overhauling data. Tax Enquiry Accounts

    ReplyDelete
  4. Much obliged to you again for all the information you distribute,Good post. I was exceptionally keen on the article, it's entirely rousing I ought to concede. I like going to you site since I generally run over fascinating articles like this one.Great Job, I incredibly acknowledge that.Do Keep sharing! Respects, HMRC Tax Investigation Accountants

    ReplyDelete
  5. I am constantly stunned by the measure of data accessible on this subject. What you exhibited was very much explored and eloquent so as to get your stand on this crosswise over to every one of your perusers. Tax Enquiry

    ReplyDelete
  6. On the off chance that more individuals that compose articles truly fretted about composing incredible substance like you, more perusers would be occupied with their works. Much obliged to you for thinking about your substance. Let Campaign Tax Enquiry

    ReplyDelete
  7. There are actually lots of particulars like that to take into consideration. That may be a nice level to bring up. I provide the ideas above as general inspiration but clearly there are questions just like the one you convey up the place a very powerful thing will probably be working in sincere good faith. I don?t know if greatest practices have emerged round issues like that, but I’m sure that your job is clearly recognized as a good game. Both boys and girls feel the impression of just a moment’s pleasure, for the rest of their lives. Tax Investigation Self Employed

    ReplyDelete
  8. I think this is one of the most significant information for me. And i’m glad reading your article. But should remark on some general things, The web site style is perfect, the articles is really great : D. Good job, cheers Let Campaign Tax Enquiry Accountants

    ReplyDelete
  9. Much obliged to you for some other educational online journal. Where else would I be able to understand that kind of data written in such a perfect means? I have a mission that I"m a few seconds ago taking a shot at, and I have been at the post for such data. Let Property Campaign

    ReplyDelete
  10. I am upbeat to discover this post exceptionally helpful for me, as it contains part of data. I generally want to peruse the quality substance and this thing I found in you post. A debt of gratitude is in order for sharing. Let Property Campaign Accountants

    ReplyDelete