简体   繁体   English

如何区分用户是用手指还是苹果铅笔点击屏幕?

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

The App supports iPad Pro and it has to work with the Apple Pencil.该应用程序支持 iPad Pro,并且必须与 Apple Pencil 配合使用。 What I would like to do is to differentiate whether the user is using the Apple Pencil or his finger.我想做的是区分用户是使用 Apple Pencil 还是他的手指。

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.我找到了UITouchTypeStylus属性,但不知道它是如何工作的。

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.我的主要问题是示例很少,而且它们是用 swift 编写的,但我正在使用目标 C。而且我无法真正理解这些示例。

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.不幸的是,我现在真的没有时间学习 swift ......这完全阻止了我。

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.所以,如果有人可以给我一些 Objective C 中的样本,让我可以使用 Apple Pencil,或者只是一个开始。 A tutorial on a web site would be perfect also but I don't think there are any.网站上的教程也很完美,但我认为没有。

To check if a UITouch is using the stylus touch type in Objective-C:要检查UITouch是否在 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:您可以尝试添加第二个长按手势识别器并在每个手势识别器上设置allowedTouchTypes属性以识别手写笔或直接触摸:

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.如果这不起作用,您必须向长按手势添加一个委托,并在gestureRecognizer: shouldReceiveTouch:方法中,检查并存储触摸类型,并在手势操作触发时使用它。

The UITouch class in iOS 9.1 has a touch property which returns the type: iOS 9.1 中的UITouch类有一个返回类型的touch属性:

typedef enum {
    UITouchTypeDirect,
    UITouchTypeIndirect,
    UITouchTypeStylus       // THIS ONE
} UITouchType;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM