does the accelerometer work for the iphone/ipad simulator?

我们两清 提交于 2020-12-06 06:41:41

问题


From what I can tell, my app should be firing accelerometer events while Im using the iPad simulator in XCode, but its not.

I have googled around and it somewhat seems that the accelerometer is not implemented in the simulator, is this correct? If so, why on earth would they have a "Hardware->Shake Gesture" menu option?

My code is as follows:

.h file:

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

then the .m file:

@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

Does this look right?


回答1:


There's no accelerator in the simulator.

The "Hardware → Shake Gesture" is generated by directly sending a UIEvent to the active application.

Although Shake Gesture is implemented using the accelerator on the device, conceptually they are two different kind of user input — the gesture is an event, the acceleration is continuous variation. Thus the Shake Gesture menu item exists for the apps which only use such gesture (e.g. Shake to Undo) but do not need to know the exact acceleration vector.




回答2:


Accelerometer does not work on iPhone/iPod Simulator. Neither "Hardware->Shake Gesture" use accelerometer nor the orientation change of safari on simulator.



来源:https://stackoverflow.com/questions/2762768/does-the-accelerometer-work-for-the-iphone-ipad-simulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!