How to differentiate whether a user is tapping the screen with his finger or an apple pencil?

帅比萌擦擦* 提交于 2019-11-29 07:29:51

To check if a UITouch is using the stylus touch type in Objective-C:

if (touch.type == UITouchTypeStylus) {
    // Do stuff
}

If you're not handling touches directly, but using a gesture recognizer, then it is a little more complicated.

You could try adding a second long press gesture recogniser and setting the allowedTouchTypes property on each one to recognise stylus or direct touches:

longPressGestureFinger.allowedTouchTypes = @[@(UITouchTypeDirect)];
longPressGesturePencil.allowedTouchTypes = @[@(UITouchTypeStylus)];

If that doesn't work, you would have to add a delegate to the long press gesture, and in the gestureRecognizer: shouldReceiveTouch: method, check and store the type of touch and use this when the gesture action fires.

The UITouch class in iOS 9.1 has a touch property which returns the type:

typedef enum {
    UITouchTypeDirect,
    UITouchTypeIndirect,
    UITouchTypeStylus       // THIS ONE
} UITouchType;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!