首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iOS中使用加速度计需要帮助

在iOS中使用加速度计需要帮助
EN

Stack Overflow用户
提问于 2012-12-06 01:21:25
回答 2查看 265关注 0票数 3

我正在开发一款应用程序,它会为用户持有iOS设备的每个位置(站着,前/后躺,或侧躺)播放蜂鸣声。目前,当用户侧着设备时,我可以播放声音,但问题是,因为我将加速度计数值与滑块链接在一起,所以嘟嘟声是连续的(即,只要用户侧着设备,它就会播放声音),而不是只播放一次。

我希望用户只需将设备侧向固定,然后发出一声嘟嘟声,允许用户依次将设备放在其他位置,并等待另一声嘟嘟声。我希望用户一步一步地走,一次一个地将设备放在每个位置,只有在听到嘟嘟声后才能移动到下一个位置。

下面是我正在使用的代码:

代码语言:javascript
复制
- (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()方法的代码(仅供参考,方法pushBap_ all /pushBap_all/pushBap_ for完全相同):

代码语言:javascript
复制
-(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];
}

有没有人能搞清楚这里的问题是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-06 01:48:34

我认为你应该使用内置的方向通知,而不是手动轮询加速度计。如果您需要FaceUp和FaceDown方向,您可以使用类似下面的内容。或者你可以使用第二种方法,简单的风景,肖像。

依赖于设备方向的第一个方法。如果您需要FaceUp或FaceDown方向,或者如果您没有UIViewController,请注意这一点。

代码语言:javascript
复制
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
   addObserver:self selector:@selector(orientationChanged:)
   name:UIDeviceOrientationDidChangeNotification
   object:[UIDevice currentDevice]];

下面是构建的方法。

代码语言:javascript
复制
- (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;
   };
}

依赖于interfaceOrientations of a UIViewController的第二个方法

代码语言:javascript
复制
- (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;
    }
}
票数 2
EN

Stack Overflow用户

发布于 2013-01-03 05:09:00

加速计来自于加速度这个词-它不会告诉你设备的方位,它只会告诉你它在x,y和z轴上的空间移动速度。使用UIInterfaceOrientation && UIDeviceOrientation。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13728817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档