新建子类继承UITextView,子类需遵循UIGestureRecognizerDelegate协议
@interface SubTextView : UITextView<UIGestureRecognizerDelegate>
@end
@implementation SubTextView
//通过touch事件,判断是否是UITouchTypePencil
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (touch.type == UITouchTypePencil) {
self.editable = NO;
return NO;
}
self.editable = YES;
return YES;
}
//本次touch结束时,重置可编辑状态,
//避免当pencil切换手指点击唤起键盘时,需要连续点两次
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
self.editable = YES;
}
@end