2

据我所知,当我在 XCode 中使用 iPad 模拟器时,我的应用程序应该触发加速度计事件,但事实并非如此。

我用谷歌搜索了一下,似乎加速度计没有在模拟器中实现,这是正确的吗?如果是这样,他们到底为什么会有“硬件->摇动手势”菜单选项?

我的代码如下:

.h 文件:

@interface MyViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate, UIAccelerometerDelegate>{
    UIAccelerometer *accelerometer;
    //...other stuff
}
@property (nonatomic, retain) UIAccelerometer *accelerometer;
@end

然后是 .m 文件:

@implementation MyViewController
@synthesize accelerometer;
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    NSLog(@"%@", [NSString stringWithFormat:@"%@%f", @"X: ", acceleration.x]);
    NSLog(@"%@", [NSString stringWithFormat:@"%@%f", @"Y: ", acceleration.y]);
    NSLog(@"%@", [NSString stringWithFormat:@"%@%f", @"Z: ", acceleration.z]);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.accelerometer = [UIAccelerometer sharedAccelerometer];
    self.accelerometer.updateInterval = .1;
    self.accelerometer.delegate = self;
}
@end

这看起来对吗?

4

2 回答 2

5

模拟器中没有加速器。

“硬件→摇动手势”是通过直接向活动应用程序发送 UIEvent 来生成的。

虽然摇动手势是使用设备上的加速器实现的,但从概念上讲,它们是两种不同类型的用户输入——手势是一个事件,加速度是连续变化的。因此,Shake Gesture 菜单项适用于仅使用此类手势(例如摇动撤消)但不需要知道确切加速度矢量的应用程序。

于 2010-05-04T11:00:02.763 回答
1

加速度计不适用于 iPhone/iPod 模拟器。“硬件->摇动手势”既不使用加速度计,也不使用模拟器上 safari 的方向变化。

于 2010-05-04T05:27:46.937 回答