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];    
    }
}
    
已邀请:
首先,请确保您的界面采用UIAccelerobeterDelegate协议。
@interface MainViewController : UIViewController <UIAccelerometerDelegate>
现在在您的实现中:
//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的值始终是-1.0到正1.0之间的浮点。您应该调用NSLog并将其x,y和z值输出到控制台,以便对它们的含义有所了解。然后,您可以开发一种简单的方法来检测运动。     
您无法更改震动事件的工作方式。如果您需要其他东西,则必须根据[UIAccelerometer sharedAccelerometer]赋予您的x / y / z力编写自己的震动检测代码;如果将sharedAccelerometer \的委托设置为您的类,则可以获取accelerometer的accelerometer:didAccelerate:委托回调。     
如果您要重新创建摇动手势,请牢记以下几点: 摇动是在一个方向上的运动,然后是在通常相反的方向上的运动。这意味着您必须跟踪以前的加速度矢量,以便知道它们何时改变。为了确保您不会错过它,您将不得不非常频繁地从加速度计中采样。
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
}
    

要回复问题请先登录注册