Archer Chen
Archer Chen

Reputation: 13

The action of GestureRecognizer does not fire

I add the gesture to view as following steps:

@interface iCarousel () <UIGestureRecognizerDelegate>  
{ 
     UIPanGestureRecognizer * mPanGesture;  
     UITapGestureRecognizer * mTapGesture; 
}
@end  
@implementation iCarousel
- (void)setUp  
{
    _contentView = [[UIView alloc] initWithFrame:self.bounds];  

    _contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;  

    /  
    mPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(iCarouselDidPan:)];  
    mPanGesture.delegate = self;  
    [_contentView addGestureRecognizer:mPanGesture];  

    /  
    mTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iCarouselDidTap:)];  
    mTapGesture.delegate = self;  
    [_contentView addGestureRecognizer:mTapGesture];  

    /  
    self.accessibilityTraits = UIAccessibilityTraitAllowsDirectInteraction;  
    self.isAccessibilityElement = YES;  

    [self addSubview:_contentView];  
} 
- (id)initWithCoder:(NSCoder *)aDecoder  
{  
    if ((self = [super initWithCoder:aDecoder]))  
    {  
        [self setUp];  
    }  
    return self;  
}  

- (id)initWithFrame:(CGRect)frame  
{  
    if ((self = [super initWithFrame:frame]))  
    {  
        [self setUp];  
    }  
    return self;  
}  

...  


- (void)iCarouselDidTap:(UITapGestureRecognizer *)tapGesture  
{  
     ...  
}  


- (void)iCarouselDidPan:(UIPanGestureRecognizer *)panGesture  
{  
     ...  
}  

...  

@end  

Now, the method in this class

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gesture

return me YES

But, the actions iCarouselDidPan: and iCarouselDidTap: do not fire.

The information is printed in gestureRecognizerShouldBegin: method.

(lldb) po _contentView.gestureRecognizers 
<__NSArrayI 0x1572bec0>(  
<UIPanGestureRecognizer: 0x16dea750; state = Possible; view = <UIView 0x16dea640>; target= <(action=iCarouselDidPan:, target=<iCarousel 0x16dea1b0>)>>,
<UITapGestureRecognizer: 0x16e8f310; state = Possible; view = <UIView 0x16dea640>; target= <(action=iCarouselDidTap:, target=<iCarousel 0x16dea1b0>)>> 
)  
(lldb) po gesture 
<UIPanGestureRecognizer: 0x16dea750; state = Possible; view = <UIView 0x16dea640>; target= <(action=iCarouselDidPan:, target=<iCarousel 0x16dea1b0>)>> 

It's only happened on devices <4s 9.3.x , 5 10.3.1> at current moment.

Upvotes: 1

Views: 950

Answers (3)

Himanshu Moradiya
Himanshu Moradiya

Reputation: 4815

Declare UIGestureRecognizerDelegate in .h file

Gesture definition

-(UITapGestureRecognizer*)setTapGesture{

    UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc]
                                           initWithTarget:self action:@selector(handleTapWithGestureRecognizer:)];
    Tap.delegate = self;
    return Tap;

}
-(void)handleTapWithGestureRecognizer:(UITapGestureRecognizer *)TapGestureRecognizer{
    NSLog(@"tap Gesture");
}

-(UIPanGestureRecognizer*)setpanGesture{

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    return panRecognizer;

}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {

    [self.view bringSubviewToFront:recognizer.view];
     CGPoint touchLocation = [recognizer locationInView:self.view];
     self.YOURVIEW.center = touchLocation;
}

Call Gesture or add gesture to your view

[self.YOURVIEW addGestureRecognizer:[self setTapGesture]];
[self.YOURVIEW addGestureRecognizer:[self setpanGesture]];

self.YOURVIEW.userInteractionEnabled = YES;

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

Good Luck Happy Coding.

Upvotes: 0

KKRocks
KKRocks

Reputation: 8322

Possible cases for not working gestures .

1 - You need to enable interaction of view.

 [_contentView setUserInteractionEnabled:YES];

2 - Add this delegate in your class.

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

Upvotes: 0

Roman Podymov
Roman Podymov

Reputation: 4521

Try implement the following methods of protocol UIGestureRecognizerDelegate inside your iCarousel:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
       shouldReceiveTouch:(UITouch *)touch {

    return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
       shouldReceivePress:(UIPress *)press {

    return YES;
}

Upvotes: 1

Related Questions