Search code examples
objective-cuiviewuistoryboardxibuitapgesturerecognizer

Add UITapGestureRecognizer to UIVIew XIB


@interface MyCustomView() @end

@implementation MyCustomView

- (instancetype)init {
    self = [[[MyClass bundle] loadNibNamed:kOverlayNib owner:self options:nil] firstObject];
    self.layer.cornerRadius = 10;
    self.translatesAutoresizingMaskIntoConstraints = NO;
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleFingerTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(handleSingleTap:)];
    [singleFingerTap setNumberOfTapsRequired:1];
    [singleFingerTap setNumberOfTouchesRequired:1];
    [self addGestureRecognizer:singleFingerTap];

    return self; }

//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
    NSLog(@"Tappedddddddddd");
}

I have added Tap Gesture Recognizer to my kOverlayNib.xib but for some reason i can not get any response.. It is not working at all..


Solution

  • you missed some things..

    1. @interface MyCustomView() <UIGestureRecognizerDelegate> @end or anywhere you want to write.

    2. singleFingerTap.delegate = self;

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

    Now you can run your code. it works.