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

2

8

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.

Something like:

if( the user is pressing the screen with his finger){
    do something
} else if ( the user is pressing the screen with an Apple Pencil){
    do something else
}

I found the UITouchTypeStylus attribute but was not able to know how it works.

My main problem is that there are really few examples, and they are written in swift, but I am working in objective C. And I am not able to really understand these samples.

Look at this, this is a function from a sample that I found on the apple developer:

func addPointsOfType(var type: LinePoint.PointType, forTouches touches: [UITouch], toLine line: Line, currentUpdateRect updateRect: CGRect) -> CGRect {
    var accumulatedRect = CGRect.null

    for (idx, touch) in touches.enumerate() {
        let isStylus = touch.type == .Stylus // I think that what I'm looking for is something like that

        [...]
}

I don't really have the time to learn swift now, unfortunately... This is blocking me completely.

So if someone could give me some samples in Objective C that will allow me to work with the Apple Pencil, or just a beginning. A tutorial on a web site would be perfect also but I don't think there are any.

Visigoth answered 10/5, 2016 at 8:38 Comment(0)
G
14

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.

Grumpy answered 10/5, 2016 at 8:42 Comment(5)
It's showing me an error on touch : "Use of undeclared identifier touch", how can I resolve this?Visigoth
Well I was assuming you already had a method where you had one or more UITouch objects, the example code is just for testing the touch type, which is what you asked for.Grumpy
From now I was just using y finger on my application, and now I have to implement the Apple Pencil. So no, sorry, I dont have this. Can you tell me a bit on how I should use this UITouch Object please?Visigoth
You add the test above to the code you are already using to handle finger touches, to allow you to decide if it is a finger or a stylus. If you have existing touch handling code, then it would be a good idea to put it in the question. If you don't, then you've asked the wrong question.Grumpy
Are you speaking about something like a GestureRecognizer? Actually i'm using a UILongPressGestureRecognizer to open an editing window for an object. So if it's a finger, it will show a window, if it's a pencil, it will show an other window.Did you better understand my problem ? (it's sounds like there is a mistake here sorry)Visigoth
L
1

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

typedef enum {
    UITouchTypeDirect,
    UITouchTypeIndirect,
    UITouchTypeStylus       // THIS ONE
} UITouchType;
Lablab answered 10/5, 2016 at 8:42 Comment(1)
Yeah you need to test the property of the touch object, but you don't need to add that declaration, no.Lablab

© 2022 - 2024 — McMap. All rights reserved.