加速度计适用于 iphone/ipad 模拟器吗?
据我所知,当我在 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
这看起来正确吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模拟器中没有加速器。
“硬件→摇动手势”是通过直接向活动应用程序发送 UIEvent 来生成的。
虽然摇动手势是使用设备上的加速器实现的,但从概念上讲,它们是两种不同类型的用户输入——手势是一个事件,加速度是连续变化。因此,对于仅使用此类手势(例如,摇动以撤消)但不需要知道确切的加速度矢量的应用程序,存在摇动手势菜单项。
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.
加速计不适用于 iPhone/iPod 模拟器。 “硬件->摇动手势”均不使用加速度计,也不使用模拟器上 safari 的方向变化。
Accelerometer does not work on iPhone/iPod Simulator. Neither "Hardware->Shake Gesture" use accelerometer nor the orientation change of safari on simulator.