使用加速度计检测压力强度

昨天,在关于iPad 2新Garageband的演示中,Apple演示了一个有趣的function:使用加速度计检测分接压力。 (参见Garageband页面上的鼓部分。)

我想知道如果iPad平放在桌子上,那该怎么办呢。 没有运动,没有可测量的加速度,没有?

一些好的答案。 这是一些有效的代码。 我将其实现为UIGestureRecognizer的子类,以便您可以将其删除并将其附加到UIView或UIButton。 一旦触发,它将“压力”设置为介于0.0f和2.0f之间的浮点数。 您可以选择设置识别所需的最小和最大压力。 请享用。

// // CPBPressureTouchGestureRecognizer.h // PressureSensitiveButton // // Created by Anthony Picciano on 3/21/11. // Copyright 2011 Anthony Picciano. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met: // 1. Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // #import  #define CPBPressureNone 0.0f #define CPBPressureLight 0.1f #define CPBPressureMedium 0.3f #define CPBPressureHard 0.8f #define CPBPressureInfinite 2.0f @interface CPBPressureTouchGestureRecognizer : UIGestureRecognizer  { @public float pressure; float minimumPressureRequired; float maximumPressureRequired; @private float pressureValues[30]; uint currentPressureValueIndex; uint setNextPressureValue; } @property (readonly, assign) float pressure; @property (readwrite, assign) float minimumPressureRequired; @property (readwrite, assign) float maximumPressureRequired; @end // // CPBPressureTouchGestureRecognizer.h // PressureSensitiveButton // // Created by Anthony Picciano on 3/21/11. // Copyright 2011 Anthony Picciano. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met: // 1. Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // #import  #import "CPBPressureTouchGestureRecognizer.h" #define kUpdateFrequency 60.0f #define KNumberOfPressureSamples 3 @interface CPBPressureTouchGestureRecognizer (private) - (void)setup; @end @implementation CPBPressureTouchGestureRecognizer @synthesize pressure, minimumPressureRequired, maximumPressureRequired; - (id)initWithTarget:(id)target action:(SEL)action { self = [super initWithTarget:target action:action]; if (self != nil) { [self setup]; } return self; } - (id)init { self = [super init]; if (self != nil) { [self setup]; } return self; } - (void)setup { minimumPressureRequired = CPBPressureNone; maximumPressureRequired = CPBPressureInfinite; pressure = CPBPressureNone; [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0f / kUpdateFrequency]; [[UIAccelerometer sharedAccelerometer] setDelegate:self]; } #pragma - #pragma UIAccelerometerDelegate methods -(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { int sz = (sizeof pressureValues) / (sizeof pressureValues[0]); // set current pressure value pressureValues[currentPressureValueIndex%sz] = acceleration.z; if (setNextPressureValue > 0) { // calculate average pressure float total = 0.0f; for (int loop=0; loop= minimumPressureRequired && pressure <= maximumPressureRequired) self.state = UIGestureRecognizerStateRecognized; else self.state = UIGestureRecognizerStateFailed; } } currentPressureValueIndex++; } #pragma - #pragma UIGestureRecognizer subclass methods - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { setNextPressureValue = KNumberOfPressureSamples; self.state = UIGestureRecognizerStatePossible; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { self.state = UIGestureRecognizerStateFailed; } - (void)reset { pressure = CPBPressureNone; setNextPressureValue = 0; currentPressureValueIndex = 0; } @end 

我认为铝制shell和工作台不能防止非常小的运动,并且传感器非常敏感。

或者,iPad站在智能机盖上的车库乐队用户体验更好。

那么简单地做这个实验:

使用XCode打开XCode包中包含的AccelerometerGraph教程应用程序。 然后启动应用程序(高通filter,更好地使用自适应过滤):您将看到蓝线根据应用程序的强度而变化。 当然,这会受到表格中颤抖的影响,这会增加测量的噪音,但您仍然可以通过触摸事件检查加速度计数据来过滤它。

因此,使用加速度计可以进行这种压力检测。

点击区域大小检测? (更强的水龙头,更大的指纹)点击动态?

只是想。

viggio24是对的。 手指区域感应工作正常。 (我在这里有一些post,有一些简单的代码来获得它)。 唯一的问题是,如果您启用它后,不清楚后果是什么; 我们假设它不会在最好的情况下被批准。

也许这是由于更灵敏的陀螺仪? 结合Accelerometer数据,可以相当容易地确定运动中的微小变化。 只是一种预感。 除非他们没有告诉我们什么,这不是第一次。

即使SDK仅向我们公开触摸区域,我相信屏幕硬件为操作系统提供了更多信息:触摸像素(用于区域检测)和触摸持续时间。 一起(和实验)他们可以给你一个很好的估计施加的压力。 如果Apple向开发人员发布这种经验压力估计,那就太好了。

使用AccelerometerGraph示例应用程序自己尝试一下。 设备和桌面具有有限的刚度,因此您可能会在图表中看到很少的光点。

添加:

如果您自己从源代码编译Apple的AccelerometerGraph图形示例,则可以在垂直轴上调高增益并查看较小加速度的光点。

Interesting Posts