3

私は、ユーザーがiOSデバイスを持っている位置(立っている、正面/背面、または横になっている)ごとにビープ音を鳴らすアプリに取り組んでいます。現時点では、ユーザーがデバイスを横にした状態で音を鳴らすことができますが、問題は、加速度計の値がスライダーにリンクされているため、ビープ音が連続して鳴る(つまり、次のように音が鳴る)ことです。ユーザーがデバイスを横に持っている限り)、1回だけではありません。

ユーザーがデバイスを横に固定するだけで、ビープ音が1回鳴り、デバイスを他の位置に順番に保持して、次のビープ音が鳴るのを待つようにしたいと思います。ユーザーが一歩一歩進んで、デバイスを一度に1つずつ各位置に保持し、ビープ音が鳴った後でのみ次の位置に移動してほしい。

これが私が使っているコードです:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{

    NSLog(@"(%.02f, %.02f, %.02f)", acceleration.x, acceleration.y, acceleration.z);
    slider.value = acceleration.x;

    if (slider.value == -1)
        [self pushBeep];

    else if (slider.value == 0.00)
        [self pushBap];

    else if (slider.value == 1)
        [self pushBop];

...

これが私のpushBeep()メソッドのコードです(参考までに、pushBeep / pushBap / pushBopメソッドはすべて同じです):

-(void) pushBeep {

    NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"beep-7" ofType:@"wav"];
    NSURL *soundURL = [NSURL fileURLWithPath:soundPath];

    NSError *ierror = nil;
    iPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&ierror];

    [iPlayer play];
}

誰かがここで何が問題なのか理解できますか?

4

2 に答える 2

2

加速度計を手動でポーリングする代わりに、組み込みの方向通知を使用する必要があると思います。FaceUpとFaceDownの向きが必要な場合は、次のようなものを使用できます。または、2番目の方法を使用して、単純に横向き、縦向きにすることもできます。

デバイスの向きに依存する最初の方法。FaceUpまたはFaceDownの向きが必要な場合、またはUIViewControllerがない場合に重要です。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
   addObserver:self selector:@selector(orientationChanged:)
   name:UIDeviceOrientationDidChangeNotification
   object:[UIDevice currentDevice]];

そして、これが構築する方法です。

- (void) orientationChanged:(NSNotification *)note
{
   UIDevice * device = note.object;
   switch(device.orientation)
   {
       case UIDeviceOrientationPortrait:
       /* Play a sound */
       break;

       case UIDeviceOrientationPortraitUpsideDown:
       /* Play a sound */
       break;

       // ....

       default:
       break;
   };
}

UIViewControllerのinterfaceOrientationsに依存する2番目のメソッド。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
            /* Play a Sound */
            break;

        case UIInterfaceOrientationPortrait:
            /* Play a Sound */
            break;

            // .... More Orientations

        default:
            break;
    }
}
于 2012-12-05T17:48:33.600 に答える
0

加速度計は、加速度という言葉に由来します。デバイスの向きはわかりません。x、y、z軸の空間でのみ移動した速度がわかります。UIInterfaceOrientation&&UIDeviceOrientationを使用します。

于 2013-01-02T21:09:00.083 に答える