在 Javascript 中测试 iPhone 3GS 设备方向事件的硬件支持
我试图在 Javascript 中使用 HTML5 deviceOrientation 事件,当用户围绕他旋转 iPhone 时旋转图像。
我使用此代码来检测陀螺仪何时移动:
window.addEventListener('deviceorientation', function (e) {
alpha = e.alpha;
beta = e.beta;
gamma = e.gamma;
}, true);
它在 iPhone 4+ 和 iPad 2 上确实运行良好,但 3GS(和较旧的 iPhone)上没有陀螺仪。这就是为什么我要这样测试 deviceOrientationSupport :
if(window.DeviceOrientationEvent){ // gyroscope support
alert('DeviceOrientationEvent support OK');
} else {
alert('DeviceOrientationEvent support KO');
}
我在许多网站或论坛上看到过这段代码,但我的问题是,在 IOS 5.0.1 下使用我的 iPhone 3GS,此测试表明我:deviceOrientationSupport OK!
我认为此测试仅在 Safari 能够处理这些事件时进行检查:(
所以我的问题是,是否可以添加一个测试来了解硬件是否可以触发方向事件?
谢谢!
I'm trying to use HTML5 deviceOrientation events in Javascript to rotate an image when the user rotate his iPhone around him.
I use this code to detect when the gyroscope is moving :
window.addEventListener('deviceorientation', function (e) {
alpha = e.alpha;
beta = e.beta;
gamma = e.gamma;
}, true);
It really works well on iPhone 4+ and iPad 2, but there's no gyroscope on the 3GS (and older iPhones). That's why I'm testing the deviceOrientationSupport like this :
if(window.DeviceOrientationEvent){ // gyroscope support
alert('DeviceOrientationEvent support OK');
} else {
alert('DeviceOrientationEvent support KO');
}
I've seen this code on many websites or forums, but my problem is that with my iPhone 3GS under IOS 5.0.1, this test indicates me : deviceOrientationSupport OK !
I think that this test check only if Safari is able to handle these events :(
So my question is, is it possible to add a test to know if the hardware can fire orientation events ?
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我希望这对您来说还不算太晚,但是 iOS 4.2+ 版本的 Safari 将在 iPhone 3GS(或其他没有陀螺仪的设备)上注册 DeviceOrientationEvent,这不是令人沮丧吗?
底线是,DeviceOrientation 不适用于 iPhone 3GS。但正如有人提到的,DeviceMotionEvent 可以工作,但您必须以与使用带有陀螺仪的设备不同的方式访问事件数据(我知道很愚蠢!)。
第一步:我在混合中添加了一个变量来捕获 OrientationEvent 是否实际上使用任何非空数据触发(就像有陀螺仪一样)。
现在您知道该事件是否确实有陀螺仪数据了!因此,如果您想默认为其他内容(即 window.DeviceMotionEvent),您可以使用条件。
我在 iPhone 3GS(无陀螺仪)和 iPad 2(陀螺仪)的 Mobile Safari 以及 Macbook Pro(陀螺仪)上的 Chrome 上进行了测试。似乎有效。
现在,如果您想在方向数据不可用的情况下获取 DeviceMotionEvent 数据作为替代方案,那么...
这应该涵盖大多数带有 webkit 浏览器的设备的基础...我希望。尚未在任何 Android 设备上测试过。
应该注意的是,每个事件返回不同的数字,因此您可能需要做一些工作来标准化它们。但这是访问它们的基础知识。
让我知道这是否有帮助!
I hope this isn't coming at you too late, but isn't it frustrating that Safari for iOS 4.2+ will register DeviceOrientationEvent on an iPhone 3GS (or other devices without a gyroscope)?
Bottom Line, DeviceOrientation does not work with the iPhone 3GS. But as someone mentioned, DeviceMotionEvent works, but you have to access the event data in a different way than you would with a device with a gyroscope (silly I know!).
First Step: I added a variable in the mix to capture whether or not the OrientationEvent was actually firing with any non-null data (as it would if there were a gyroscope).
Now you know whether or not the event actually has gyroscope data or not! So if you want to default to something else (ie. window.DeviceMotionEvent) you can use a conditional.
I tested this on Mobile Safari for iPhone 3GS (no gyroscope) and iPad 2 (gyroscope) and Chrome on my Macbook Pro (gyroscope). Seems to work.
Now, if you want to get the DeviceMotionEvent data as an alternative if the Orientation data is not available, then...
This should cover your bases for most devices with a webkit browser... I hope. Havent tested it on any Android devices.
It should be noted, each event returns different numbers, so you might need to do some work to normalize them. But this is the basics of how you access them.
Let me know if this helps!
在遇到与上面相同的问题后,我也遇到了旧版 iPad/iPhone 的问题,甚至没有调用 addEventListener 上的函数。
因此,我发现以下方法对我来说效果更好,并且消除了不受支持的设备通过的任何机会(这是带有加载屏幕的,因此在完成完整测试之前确实需要一段时间间隔。所以并不适合所有人)。
After having the same issue as above I also had problems with the older iPad/iPhones not even calling the function on addEventListener.
So I found that the following worked a lot better for me and eliminated any chance of an unsupported device getting through (this was sat with a load screen so it did require an interval before completing its full test. So not for everyone).
虽然 3GS 上没有陀螺仪,但它完全能够使用内置的加速计来检测设备方向的变化。iOS 自第一代 iPhone 以来就提供了这种支持。
While there is no Gyroscope on the 3GS, it is perfectly capable of detecting device orientation changes using the accelerometer it has build in. iOS has had this support since the first iPhone.
我有一个类似的问题,我的桌面也接受了 window.DeviceOrientationEvent 事件。我使用以下代码来检查移动设备中的方向支持:
JSFiddle: http://jsfiddle.net/7L5mg8hj /1/]
I had a similar problem that my Desktop also accepted the window.DeviceOrientationEvent event. I used the following code to check for orientation support in mobile devices:
JSFiddle: http://jsfiddle.net/7L5mg8hj/1/]