iOS开发-禁用UITextView中ApplePencil手写

2024-01-10  本文已影响0人  黑色幽默_0d96

新建子类继承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
上一篇 下一篇

猜你喜欢

热点阅读