如何区分用户是用手指还是用铅笔敲击屏幕? [英] How to differentiate whether a user is tapping the screen with his finger or an apple pencil?

查看:586
本文介绍了如何区分用户是用手指还是用铅笔敲击屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该应用程序支持iPad Pro,它必须与Apple Pencil一起使用。我想做的是区分用户是使用Apple Pencil还是他的手指。

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.

类似于:

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
}

我找到 UITouchTypeStylus 属性但无法知道它是如何工作的。

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

我的主要问题是实例很少,而且它们是用swift编写,但我在目标C中工作。我无法真正理解这些样本。

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

        [...]
}

我现在没有时间学习swift,不幸的是......这完全阻止了我。

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

所以如果有人可以给我一些目标C的样本,这些样本可以让我使用Apple Pencil,或者只是一个开头。网站上的教程也很完美,但我认为没有。

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.

推荐答案

检查是否 UITouch 在Objective-C中使用手写笔触摸类型:

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.

您可以尝试添加第二个长按手势识别器并在每个上设置 allowedTouchTypes 属性以识别手写笔或直接接触:

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)];

如果这不起作用,则必须为长按手势添加委托,并且在 gestureRecognizer:shouldReceiveTouch:方法中,检查并存储触摸类型,并在手势动作触发时使用此方法。

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.

这篇关于如何区分用户是用手指还是用铅笔敲击屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆