移动端iOS更新到13版本以后 许多全景图等场景陀螺仪失效解决方案如下:
- 需要HTTPS协议
- 隐私设置开启运动传感器
- 监听事件代码调整,iOS13以后会新增window.DeviceOrientationEvent的API,orientationchange的事件用于处理横竖屏转换,没用到可忽略。要额外注意的是其触发条件与音视频自动播放的安全协议相同,需要用户交互事件的主动触发例如点击事件之后。监听添加代码如下:
// iOS 13+
if ( window.DeviceOrientationEvent !== undefined && typeof window.DeviceOrientationEvent.requestPermission === 'function' ) {
window.DeviceOrientationEvent.requestPermission().then( function ( response ) {
if ( response == 'granted' ) {
window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent, false );
window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false );
}
} ).catch( function ( error ) {
console.error( 'Unable to use DeviceOrientation API:', error );
} );
} else {
window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent, false );
window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false );
}