加速度计-未检测到震动-可可触摸

| 你好 我试图获取它,以便当用户摇动设备时声音会播放。但是,当我摇晃它时,我会被踢出应用程序 这是我使用的代码
-(BOOL)canBecomeFirstResponder {
    return YES;
}


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

- (void)viewWillDisappear:(BOOL)animated {
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
}

- (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];    

    }

}
已邀请:
该应用程序不会因震动事件而崩溃。我刚刚使用以下代码创建了一个小型测试项目:
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

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

    [self becomeFirstResponder];
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if(motion == UIEventSubtypeMotionShake)
    {
        NSLog(@\"Shake is working\");
    }
}
这行为正确。我建议注释掉震动代码,并用一个简单的NSLog替换它,以确保震动正常工作,然后从那里开始,一次测试一个。 另外,在您的viewDidDisappear代码中,我颠倒了这两行代码的顺序。 (几乎)总是,首先进行[super ..]调用。

要回复问题请先登录注册