Viral Narshana
Viral Narshana

Reputation: 1877

UIGestureRecognizer not working correctly in iphone

I am using UIPanGestureRecognizer to move imageview in the view and UISwipeGestureRecognizer to remove imageview from view. here is my code.

- (void)viewDidLoad
{

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
panRecognizer.delegate = self;
[imgView1 addGestureRecognizer:panRecognizer];
[panRecognizer release];


UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeRecognizer.delegate = self;
swipeRecognizer.direction = (UISwipeGestureRecognizerDirectionLeft | 
                             UISwipeGestureRecognizerDirectionRight);
[imgView1 addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
}

-(void)handleSwipe:(UITapGestureRecognizer *)recognizer{
NSLog(@"swipe");
UIView *viewGes = [recognizer view];

[viewGes removeFromSuperview];
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {

NSLog(@"handlePan");
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
                                     recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

if (recognizer.state == UIGestureRecognizerStateEnded) {

    CGPoint velocity = [recognizer velocityInView:self.view];
    CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
    CGFloat slideMult = magnitude / 200;

    float slideFactor = 0.1 * slideMult; // Increase for more of a slide
    CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor), 
                                     recognizer.view.center.y + (velocity.y * slideFactor));
    finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
    finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);

    [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        recognizer.view.center = finalPoint;
    } completion:nil];
}
}

but my problem swipe gesture is not working correctly. some times it is getting call and some time not. some time slide move the image then also it delete the image. is any one have idea to handle both gestures together correctly ?

Upvotes: 0

Views: 1388

Answers (2)

Islam Adel
Islam Adel

Reputation: 575

Double Check that all your image views user interaction is enabled like that :

[imgView1 setUserInteractionEnabled:YES];

Upvotes: 0

Rok Jarc
Rok Jarc

Reputation: 18875

Make sure your class conforms to <UIGestureRecognizerDelegate> protocol.

Try adding this delegate method (to the class where you use this recognizers):

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

Upvotes: 2

Related Questions