- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
昨天,在有关适用于 iPad 2 的全新 Garageband 的演示中,Apple 演示了一个有趣的功能:使用加速度计检测敲击压力。 (参见 Garageband page 上的鼓部分。)
我想知道如果 iPad 平放在 table 上,它应该如何工作。没有运动,没有可测量的加速度,不是吗?
最佳答案
一些很好的答案。这是一些工作代码。我将其实现为 UIGestureRecognizer 的子类,这样您就可以将其放入并附加到 UIView 或 UIButton。一旦触发,它将“压力”设置为 0.0f 和 2.0f 之间的 float 值。您可以选择设置识别所需的最小和最大压力。享受吧。
//
// 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 <UIKit/UIKit.h>
#define CPBPressureNone 0.0f
#define CPBPressureLight 0.1f
#define CPBPressureMedium 0.3f
#define CPBPressureHard 0.8f
#define CPBPressureInfinite 2.0f
@interface CPBPressureTouchGestureRecognizer : UIGestureRecognizer <UIAccelerometerDelegate> {
@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 <UIKit/UIGestureRecognizerSubclass.h>
#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<sz; loop++) total += pressureValues[loop];
float average = total / sz;
// start with most recent past pressure sample
if (setNextPressureValue == KNumberOfPressureSamples) {
float mostRecent = pressureValues[(currentPressureValueIndex-1)%sz];
pressure = fabsf(average - mostRecent);
}
// caluculate pressure as difference between average and current acceleration
float diff = fabsf(average - acceleration.z);
if (pressure < diff) pressure = diff;
setNextPressureValue--;
if (setNextPressureValue == 0) {
if (pressure >= 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
关于ios - 利用加速度计检测敲击压力强度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5281568/
大家早上好! 我遇到以下问题:我有传感器加速度计并添加了一个可以帮助我输入另一个 Intent 的按钮,但不知道为什么这个按钮不起作用!有什么建议吗?谢谢帮助!! 程序是这样的: package co
我想使用手机加速计来滚动球中的球。运动有效正确的是,问题在于球撞到墙时。我怎样才能顺利滚动球沿着大球内侧滑动的动画? 这是我当前移动球并检查交点的代码: onSuccess: function
我是 Core Motion 的新手,我很困惑。有人可以用简单的术语解释这些输入衡量的是什么以及它们如何有用吗? 最佳答案 加速度计通过“感觉”施加到设备上的运动力来测量相对于重力的运动。运动力可以描
尝试测量基于距离的加速度(移动加速度计)。如果那是真的 Accelerometer { id: accel dataRate: 1000 / 25 onReadingChang
我需要实现一个震动识别器,我正在使用设备上的加速度计来实现这一点。但是,当我检查从传感器获得的值时,它们似乎因设备而异。例如,我在一台设备上得到 0-8 的值范围作为力(经过一些计算),在另一台设备上
我一直在尝试使用 sensors 实现陀螺仪图像查看器包,然而,结果似乎非常滞后。我在 YouTube 上发现了一个类似的项目它试图实现类似的目标,但正如您在视频中看到的那样,动画也非常滞后。 以下代
我必须找到解决问题的最佳方法,尝试识别 body 运动(口袋里有 iPhone),例如行走、停止、左转/右转、坐下。 我正在考虑只是启发式地查找与每个操作相对应的数据,然后根据该数据(带有阈值)检查传
如何平滑 iPhone/iPod Touch 的加速计? 最佳答案 您可以在使用之前对传入数据应用过滤器来平滑加速度计数据。您要做的第一件事是为您的过滤器设置一个常量。 #define kFilter
我想使用陀螺仪、加速度计和磁力计来获得 3 轴完全平滑的旋转。 第一个问题是我的输出有很大的噪音,我想知道如何消除它。 第二个问题是陀螺仪漂移,我认为应该用卡尔曼滤波器来解决。 第三个问题是加速度计。
我编写了让玩家左右移动的代码,当玩家向左移动时它有一个动画,当他向右移动时它有另一个动画。 这是代码(dt 是 deltaTime): // Moving player desktop
我最近在我的摩托罗拉 Droid 上使用 Android SDK 和适用于 Android SDK 的 Adobe AIR 来研究 Android 加速度计。我注意到加速度计工作得很好,但我想知道
我想创建一个用户在开车时无法打开短信的应用程序(意味着通过检查 android 设备特定速度的加速度计)。在 android 中是否可行,如果是,那么我们如何做到这一点。谢谢。 .尼山贾恩 最佳答案
我正在开发一个 iPhone 应用程序,我们试图在其中计算移动汽车的加速度。类似的应用程序已经完成了这一点(Dynolicious),但不同的是,这个应用程序是为一般城市驾驶而设计的,而不是在拖拉带上
当水平放置在平面上时,我的 Galaxy S4 报告恒定加速度 X:0.2 Y:-0.1 Z:10.4 由于它平稳地水平放置在 table 上,因此期望报告 X:0 Y:0 Z:10.4 通过检查与我
我目前正在开发一个 android 应用程序。我必须用事件时间记录加速度计传感器事件坐标。我得到了像“3497855005850”这样的传感器事件时间戳,但我无法将事件时间戳转换为用户可读的日期时间格
我正在尝试构建一个应用程序,用于从我的手机上读取加速度计的值,该应用程序仅支持 Android 2.1。 如何使用兼容 2.1 的代码从加速度计中读取数据? 最佳答案 从这里开始: public cl
我有两个问题: 1. 是否可以检索 b2Body 的当前 X 坐标?我知道 API GetPosition 但这还不够具体,我需要专门的 X 坐标。 2. 是否可以在我的 UIAcceleromete
Phonegap 加速度计似乎返回如下 y 值: // +10 y axis = straight up = 0/360 deg // -10 y axis = straight down = 180
我一直在使用这个代码 Android: I want to shake it实现摇动运动检测器。但实际上当我在我的智能手机上运行它时,它什么也没做。我尝试使用断点来检查,但它永远不会进入 if (mA
我正在 Android 中开发一个应用程序以及 Android Wear 支持。在查看来自 https://developer.android.com/training/building-wearab
我是一名优秀的程序员,十分优秀!