Add UITapGestureRecognizer to UITextView without blocking textView touches

后端 未结 3 1116
慢半拍i
慢半拍i 2020-12-05 15:31

How can I add a UITapGestureRecognizer to a UITextView but still have the touches getting through to the UITextView as normal?

Cur

3条回答
  •  既然无缘
    2020-12-05 16:12

    In case anyone came here looking for @Zell B.'s answer in Objective C, here's the code:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(textViewTapped:)];
        tap.delegate = self; 
        tap.numberOfTapsRequired = 1; 
       [self.textView addGestureRecognizer:tap];
    }   
    
    - (void)textViewTapped:(UITapGestureRecognizer *)tap {
        //DO SOMTHING 
    }
    
    #pragma mark - Gesture recognizer delegate
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
        return YES; 
    }
    

    PS: Don't forget < UIGestureRecognizerDelegate >

提交回复
热议问题