如何区分用户是否用手指或苹果铅笔拍屏幕?

该应用程序支持iPad Pro,它必须使用Apple Pencil。 我想要做的是区分用户是否使用苹果铅笔或他的手指。

就像是:

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属性,但无法知道它是如何工作的。

我的主要问题是,这里只有很less的例子,它们是用快速编写的,但是我的工作是客观的,而且我不能真正理解这些例子。

看看这个,我从苹果开发者那里find一个样本,这是一个函数:

 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 [...] } 

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

所以如果有人能够给我一些Objective C的样本,让我用苹果铅笔工作,或者只是一个开始。 在网站上的教程将是完美的,但我不认为有任何。

在此先感谢,每一个帮助将被接受! 🙂

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

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

如果你不是直接处理触摸,而是使用手势识别器,那么它更复杂一点。

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

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

如果这不起作用,您将不得不向长按手势添加一个委托,并在gestureRecognizer: shouldReceiveTouch:方法中检查并存储触摸的types,并在触发手势动作时使用它。

iOS 9.1中的UITouch类有一个touch属性,它返回types:

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