移动端浏览器IOS13以上,陀螺仪调用注意事项

移动端iOS更新到13版本以后 许多全景图等场景陀螺仪失效解决方案如下:

  1. 需要HTTPS协议
  2. 隐私设置开启运动传感器
  3. 监听事件代码调整,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 );

        }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容