将UITapGestureRecognizer添加到UIVIew XIB

时间:2018-04-26 13:18:25

标签: objective-c uiview uistoryboard xib uitapgesturerecognizer

@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");
}

我已将Tap Gesture Recognizer添加到我的kOverlayNib.xib但由于某种原因我无法得到任何回复..它根本不起作用..

1 个答案:

答案 0 :(得分:1)

你错过了一些东西..

  1. @interface MyCustomView() <UIGestureRecognizerDelegate> @end或您想写的任何地方。

  2. singleFingerTap.delegate = self;

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

  4. 现在您可以运行您的代码了。它有效。