Ios 通过加速计检测iPhone上任何地方的硬点击
我正在尝试检测可能出现在iPhone上任何地方的窃听,而不仅仅是iPhone屏幕。下面是一个例子,说明这是可能的 基本上,我想做的是,如果用户在iPhone上轻触3次,而手机在他的口袋里,就会发出警报。 我所取得的成就是,我可以检测到3次轻敲,但在这些情况下,我也会收到错误警报。1) 如果用户走路,2)挥动手机3)跑步。我只需要检查一下用户是否打了他的iPhone 3次 这是我的密码Ios 通过加速计检测iPhone上任何地方的硬点击,ios,iphone,objective-c,tap,Ios,Iphone,Objective C,Tap,我正在尝试检测可能出现在iPhone上任何地方的窃听,而不仅仅是iPhone屏幕。下面是一个例子,说明这是可能的 基本上,我想做的是,如果用户在iPhone上轻触3次,而手机在他的口袋里,就会发出警报。 我所取得的成就是,我可以检测到3次轻敲,但在这些情况下,我也会收到错误警报。1) 如果用户走路,2)挥动手机3)跑步。我只需要检查一下用户是否打了他的iPhone 3次 这是我的密码 - (void)accelerometer:(UIAccelerometer *)accelerometer
- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration
{
if (handModeOn == NO)
{
if(pocketFlag == NO)
return;
}
float accelZ = 0.0;
float accelX = 0.0;
float accelY = 0.0;
accelX = (acceleration.x * kFilteringFactor) + (accelX * (1.0 - kFilteringFactor));
accelY = (acceleration.y * kFilteringFactor) + (accelY * (1.0 - kFilteringFactor));
accelZ = (acceleration.z * kFilteringFactor) + (accelZ * (1.0 - kFilteringFactor));
self.z.text = [NSString stringWithFormat:@"%0.1f", -accelZ];
if((-accelZ >= [senstivity floatValue] && timerFlag) || (-accelZ <= -[senstivity floatValue] && timerFlag)|| (-accelX >= [senstivity floatValue] && timerFlag) || (-accelX <= -[senstivity floatValue] && timerFlag) || (-accelY >= [senstivity floatValue] && timerFlag) || (-accelY <= -[senstivity floatValue] && timerFlag))
{
timerFlag = false;
addValueFlag = true;
timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
}
if(addValueFlag)
{
if (self.xSwitch.on)
{
NSLog(@"X sWitch is on");
[self.accArray addObject:[NSNumber numberWithFloat:-accelX]];
}
if (self.ySwitch.on)
{
NSLog(@"Y Switch is on");
[self.accArray addObject:[NSNumber numberWithFloat:-accelY]];
}
if (self.zSwitch.on)
{
NSLog(@"Z Switch is on");
[self.accArray addObject:[NSNumber numberWithFloat:-accelZ]];
}
}
//}
}
- (void)timerTick:(NSTimer *)timer1
{
[timer1 invalidate];
addValueFlag = false;
int count = 0;
for(int i = 0; i < self.accArray.count; i++)
{
if(([[self.accArray objectAtIndex:i] floatValue] >= [senstivity floatValue]) || ([[self.accArray objectAtIndex:i] floatValue] <= -[senstivity floatValue]))
{
count++;
[self playAlarm:@"beep-1" FileType:@"mp3"];
}
if(count >= 3)
{
[self playAlarm:@"06_Alarm___Auto___Rapid_Beeping_1" FileType:@"caf"];
[self showAlert];
timerFlag = true;
[self.accArray removeAllObjects];
return;
}
}
[self.accArray removeAllObjects];
timerFlag = true;
}
-(无效)加速计:(UIAccelerator*)加速计
didAccelerate:(UIAcceleration*)加速度
{
if(handModeOn==否)
{
如果(pocketFlag==否)
返回;
}
浮动加速度z=0.0;
浮动加速度x=0.0;
浮动加速度=0.0;
accelX=(加速度.x*kFilteringFactor)+(加速度*(1.0-kFilteringFactor));
加速度=(加速度.y*kFilteringFactor)+(加速度*(1.0-kFilteringFactor));
accelZ=(加速度.z*kFilteringFactor)+(加速度*(1.0-kFilteringFactor));
self.z.text=[NSString stringWithFormat:@“%0.1f”,-accelZ];
如果(-accelZ>=[Sensitivity floatValue]&&timerFlag)| |(-accelZ=[Sensitivity floatValue]&&timerFlag)| |(-accelX=[Sensitivity floatValue]&&timerFlag)| |([[self.accArray ObjectatinIndex:i]floatValue]=3)
{
[自动播放警报:@“06_警报uuuu自动uuuu快速u嘟嘟声u 1”文件类型:@“caf”];
[自我提醒];
timerFlag=true;
[self.accArray removeAllObjects];
返回;
}
}
[self.accArray removeAllObjects];
timerFlag=true;
}
任何帮助都将不胜感激
谢谢您应该对加速计数据应用高通滤波器。这只会给您带来信号尖峰 我在“UIAccelerator高通滤波器”上快速搜索了一下,找到了几个匹配项。最简单的代码是获取加速度计输入的滚动平均值,然后从瞬时读数中减去该平均值,以找到突变。毫无疑问,还有更复杂的方法
一旦你有了识别尖锐点击的代码,你就需要手工编写代码,连续检测3次尖锐点击。一些不错的答案。下面是一些工作代码。我将其作为UIGestureRecognitizer的子类实现,这样你就可以将其放入并连接到UIView或UIButton。一旦触发,它将具有“压力”设置为介于0.0f和2.0f之间的浮动。您可以选择设置识别所需的最小和最大压力。尽情享受吧
#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
#导入
#定义CPBPressureNone 0.0f
#定义CPB压力灯0.1f
#定义CPB压力介质0.3f
#定义CPB压力硬0.8f
#定义CPB压力无穷大2.0f
@接口CPBPressureTouchGestureRecognizer:UIGestureRecognizer{
@公开的
浮子压力;
所需的浮动最小压力;
所需的浮动最大压力;
@私人的
浮子压力值[30];
单位电流压力值指数;
uint设置下一个压力值;
}
@属性(只读,分配)浮动压力;
@属性(读写、分配)要求浮点最小压力;
@属性(读写、分配)要求浮点最大压力;
@结束
//
//CPB压力触控手势识别器.h
//压力敏感按钮
//
//安东尼·皮奇亚诺于2011年3月21日创作。
//版权所有2011 Anthony Picciano。保留所有权利。
//
//以源代码和二进制形式重新分发和使用,带或不带
//如果满足以下条件,则允许进行修改
//满足以下条件:
//1.源代码的重新分发必须保留上述版权
//请注意,此条件列表和以下免责声明。
//2.以二进制形式重新分发必须复制上述版权
//请注意,此条件列表和中的以下免责声明
//随分发提供的文件和/或其他材料。
//
//本软件由作者“按原样”和任何明示或暗示的
//默示保证,包括但不限于默示保证
//对于适销性和特定用途的适用性不作任何声明。
//在任何情况下,提交人均不对任何直接、间接,
//附带、特殊、惩戒性或后果性损害(包括但不限于
//不限于,替代货物或服务的采购;使用损失,
//数据或利润;或业务中断),无论是何种原因造成的
//责任理论,无论是合同责任、严格责任还是侵权责任
//(包括疏忽或其他)因使用
//此软件,即使已告知可能发生此类损坏。
//
#进口
#导入“CPBPressureTouchGestureRecognitor.h”
#定义kUpdateFrequency 60.0f
#定义kNumberOfPressures示例3
@接口CPBPressureTouchGestureRecognitor(专用)
-(无效)设置;
@结束
@CPBPressureTouchGestureRecognitor的实现
@合成压力,所需最小压力,所需最大压力;
-(id)initWithTarget:(id)目标操作:(SEL)操作{
self=[super initWithTarget:target action:action];
if(self!=nil){
[自我设置];
}
回归自我;
}
-(id)init{
self=[super init];
if(self!=nil){
[自我设置];
}
回归自我;
}
-(无效)设置{
所需最小压力=CPB压力无;
maximumPressureRequired=CPB压力无穷大;
压力=CPB压力无;
[[UID加速度计]设置日期间隔:1.0f/kUpdateFrequency];
[[UID加速度计]setDelegate:self];
}
#布拉格马-
#pragma UIAccelerometerLegate方法
-(无效)加速计:(UIAccelerator*)加速计DID加速度:(UIAcceleration*)加速度{
int sz=(sizeof pressureValues)/(sizeof pressureValues[0]);
//设置当前压力值
pressureValues[当前PressureValueIndex%sz]=加速度.z;
如果(setNextPressureValue>0){
//计算平均压力
浮动总额
@implementation HighpassFilter
- (id)initWithSampleRate:(double)rate cutoffFrequency:(double)freq
{
self = [super init];
if (self != nil)
{
double dt = 1.0 / rate;
double RC = 1.0 / freq;
filterConstant = RC / (dt + RC);
}
return self;
}
- (void)addAcceleration:(UIAcceleration *)accel
{
double alpha = filterConstant;
if (adaptive)
{
double d = Clamp(fabs(Norm(x, y, z) - Norm(accel.x, accel.y, accel.z)) / kAccelerometerMinStep - 1.0, 0.0, 1.0);
alpha = d * filterConstant / kAccelerometerNoiseAttenuation + (1.0 - d) * filterConstant;
}
x = alpha * (x + accel.x - lastX);
y = alpha * (y + accel.y - lastY);
z = alpha * (z + accel.z - lastZ);
lastX = accel.x;
lastY = accel.y;
lastZ = accel.z;
}
- (NSString *)name
{
return adaptive ? @"Adaptive Highpass Filter" : @"Highpass Filter";
}
@end
- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration
{
if (pause)
{
return;
}
if (handModeOn == NO)
{
if(pocketFlag == NO)
return;
}
// float accelZ = 0.0;
// float accelX = 0.0;
// float accelY = 0.0;
rollingX = (acceleration.x * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
rollingY = (acceleration.y * kFilteringFactor) + (rollingY * (1.0 - kFilteringFactor));
rollingZ = (acceleration.z * kFilteringFactor) + (rollingZ * (1.0 - kFilteringFactor));
float accelX = acceleration.x - rollingX;
float accelY = acceleration.y - rollingY;
float accelZ = acceleration.z - rollingZ;
if((-accelZ >= [senstivity floatValue] && timerFlag) || (-accelZ <= -[senstivity floatValue] && timerFlag)|| (-accelX >= [senstivity floatValue] && timerFlag) || (-accelX <= -[senstivity floatValue] && timerFlag) || (-accelY >= [senstivity floatValue] && timerFlag) || (-accelY <= -[senstivity floatValue] && timerFlag))
{
timerFlag = false;
addValueFlag = true;
timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
}
if(addValueFlag)
{
[self.accArray addObject:[NSNumber numberWithFloat:-accelX]];
[self.accArray addObject:[NSNumber numberWithFloat:-accelY]];
[self.accArray addObject:[NSNumber numberWithFloat:-accelZ]];
}
}