Sunday, October 12, 2014

It's all about Gesture Recognizers

Hi Friends , I am going to discuss here Gesture Recognizers and its effect on Views for iOS.

So Lets start,

What is Gesture Recognizers ? 


Basically it is something related “Events Handling”. In one sentence Gesture Recognizers are objects which you can attached to your view,scrollview,buttons,Image view etc.

What is the use of Gesture Recognizers ? 

I am guessing that you all are familiar with UIButton and its action events like touchUpInside , touchDown etc.So something should be there to notify touchDown or touchUpInside events.

Gesture Recognizers do same thing for us with any Views.It interpret touches to determine specific gesture like swipe,pinch,rotation. You can assign Gesture to any view and they send action to particular target object. Object is your view’s view controller.

So in simple words you can tap,swipe,double tap,pinch,long press, rotate on any view and get notification with the help of Gesture Recognizers.


Lets Discuss about some Gestures, with less description :) and more examples
So I have a view  and I want to check gestures on this view .


Single Tap: 

    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] init];

    if([self respondsToSelector:@selector(handleSingleTapGesture:)]) {
        [singleTapGesture addTarget:self action:@selector(handleSingleTapGesture:)];
    }

    [view addGestureRecognizer:singleTapGesture];


- (void)handleSingleTapGesture:(UITapGestureRecognizer *)tapGestureRecognizer{
}

Double Tap: 

    UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] init];

    doubleTapGesture.numberOfTapsRequired = 2;

    if([_delegate respondsToSelector:@selector(handleDoubleTapGesture:)]) {
        [doubleTapGesture addTarget:_delegate action:@selector(handleDoubleTapGesture:)];
    }

    [view addGestureRecognizer:doubleTapGesture];

- (void)handleDoubleTapGesture:(UITapGestureRecognizer *)tapGestureRecognizer{
}

Long Press: 

    UILongPressGestureRecognizer *longpressGesture = [[UILongPressGestureRecognizer alloc] init];
    longpressGesture.minimumPressDuration = 0.5;
    
    if([_delegate respondsToSelector:@selector(handleLongpressGesture:)]) {
        [longpressGesture addTarget:_delegate action:@selector(handleLongpressGesture:)];
    }

    [view addGestureRecognizer:longpressGesture];

- (void)handleLongpressGesture:(UILongPressGestureRecognizer *)longpressGestureRecognizer{
}

Left Swipe: 

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] init];

    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;

    if([self respondsToSelector:@selector(slideToLeftGesture:)]) {
        [swipeLeft addTarget:self action:@selector(slideToLeftGesture:)];
    }

    [view addGestureRecognizer:swipeLeft];

- (void)slideToLeftGesture:(UISwipeGestureRecognizer *)swipeGestureRecognizer{
}


Right Swipe: 

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] init];

    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    
    if([self respondsToSelector:@selector(slideToRightGesture:)]) {
        [swipeRight addTarget:self action:@selector(slideToRightGesture:)];
    }
    
    [view addGestureRecognizer:swipeRight];

- (void)slideToRightGesture:(UISwipeGestureRecognizer *)swipeGestureRecognizer{
}


Up Swipe: 

    UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] init];

    swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    
    if([self respondsToSelector:@selector(slideToUpGesture:)]) {
        [swipeUp addTarget: self action:@selector(slideToUpGesture:)];
    }
    
    [view addGestureRecognizer:swipeUp];

- (void)slideToUpGesture:(UISwipeGestureRecognizer *)swipeGestureRecognizer{
}

Down Swipe: 

    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] init];
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    
    if([self respondsToSelector:@selector(slideToDownGesture:)]) {
        [swipeDown addTarget:self action:@selector(slideToDownGesture:)];
    }
    
    [view addGestureRecognizer: swipeDown];

- (void)slideToDownGesture:(UISwipeGestureRecognizer *)swipeGestureRecognizer{
}

Pan:

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] init];

    if([self respondsToSelector:@selector(moveViewWithGesture:)]) {
        [panGesture addTarget:self action:@selector(moveViewWithGesture:)];
    }

    [view addGestureRecognizer:panGesture];

- (void)moveViewWithGesture:(UIPanGestureRecognizer *)panGestureRecognizer{
}

Pinch:

    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] init];

    if([self respondsToSelector:@selector(handlePinchGesture:)]) {
        [pinchGesture addTarget:self action:@selector(handlePinchGesture:)];
    }
    
    [view addGestureRecognizer:pinchGesture];


-(void)handlePinchGesture:(UIPinchGestureRecognizer *)pinchGestureRecognizer{
}

Rotate: 

UIRotationGestureRecognizer *rotationGesture= [[UIRotationGestureRecognizer alloc] init];
    
    if([self respondsToSelector:@selector(handleRotationGesture:)]) {
        [rotationGesture addTarget:self action:@selector(handleRotationGesture:)];
    }
    
    [view addGestureRecognizer:rotationGesture];

-(void)handleRotationGesture:(UIRotationGestureRecognizer *)rotationGestureRecognizer{
}






No comments:

Post a Comment