2011-04-19 50 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值,以便您瞭解它們的含義。然後,您可以開發一種檢測運動的簡單方法。