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

前端 未结 2 1803
走了就别回头了
走了就别回头了 2020-12-10 17:20

The App supports iPad Pro and it has to work with the Apple Pencil. What I would like to do is to differentiate whether the user is using the Apple Pencil or his finger.

相关标签:
2条回答
  • 2020-12-10 17:39

    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.

    0 讨论(0)
  • 2020-12-10 17:41

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

    typedef enum {
        UITouchTypeDirect,
        UITouchTypeIndirect,
        UITouchTypeStylus       // THIS ONE
    } UITouchType;
    
    0 讨论(0)
提交回复
热议问题