2011-04-19 68 views
2

你好 我想灵敏度改变,当用户移动设备。目前它不是非常敏感,我相信它违约。我希望它更敏感,所以当用户摇动手机一点点时,声音就会播放。iPhone加速度计改变敏感度 - 可可触摸

下面是代码

感谢

- (BOOL)canBecomeFirstResponder 
{ 
    return YES; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    [self becomeFirstResponder]; 
} 

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{ 
    if(motion == UIEventSubtypeMotionShake) 
    { 
     NSString *path = [[NSBundle mainBundle] pathForResource:@"whip" ofType:@"wav"]; 
     if (theAudio) [theAudio release]; 
     NSError *error = nil; 
     theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error]; 
     if (error) 
      NSLog(@"%@",[error localizedDescription]); 
     theAudio.delegate = self; 
     [theAudio play];  
    } 
} 

回答

0

你不能改变的抖动事件是如何工作的。如果你需要不同的东西,你必须根据[UIAccelerometer sharedAccelerometer]给你的x/y/z力量编写你自己的震动检测代码。如果您将sharedAccelerometer的委托设置为您的班级,则可以获取加速度计的加速度计:didAccelerate:委托回调。

+0

哦,是不是有任何例子或任何东西? – LAA 2011-04-19 20:35:56

0

如果你想重新摇晃手势,这里有一些事情要牢记:

的摇是在一个方向的运动,然后在大体相反的方向运动。这意味着你必须跟踪你之前的加速度矢量,以便知道它们何时发生变化。所以你不要错过它,你必须经常从加速度计中取样。

CGFloat samplesPerSecond = 30; 
[[UIAccelerometer sharedAccelerometer] setDelegate: self]; 
[[UIAccelerometer sharedAccelerometer] setUpdateInterval: 1.0/samplesPerSecond]; 

然后在您的委托回调:

- (void) accelerometer: (UIAccelerometer *) accelerometer didAccelerate: (UIAcceleration *) acceleration { 
    // TODO add this acceleration to a list of accelerations and keep the most recent (perhaps 30) 
    // TODO analyze accelerations and determine if a direction change occurred 
    // TODO if it did, then a shake occurred! Clear the list of accelerations and perform your action 
} 
4

首先,确保你的接口采用UIAccelerobeterDelegate协议。

//get the accelerometer 
self.accelerometer = [UIAccelerometer sharedAccelerometer]; 
self.accelerometer.updateInterval = .1; 
self.accelerometer.delegate = self; 

实现委托方法:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 
{ 
    float x = acceleration.x; 
    float y = acceleration.y; 
    float b = acceleration.z; 

    // here you can write simple change threshold logic 
    // so you can call trigger your method if you detect the movement you're after 
} 

值,对X,Y和Z加速度计回报总是会之间的浮动

@interface MainViewController : UIViewController <UIAccelerometerDelegate> 
在您的实现

现在-1.0和正数1.0。您应该调用NSLog并输出到控制台的x,y和z值,以便您了解它们的含义。然后,您可以开发一种检测运动的简单方法。