Detect iOS device orientation lock

前端 未结 3 701
青春惊慌失措
青春惊慌失措 2020-12-30 09:59

How can I programmatically check whether the device orientation is locked in iOS? I see nothing in UIDevice. I know that this is supposed to be transparent to the app. But I

3条回答
  •  盖世英雄少女心
    2020-12-30 10:25

    You can use the UIAccelerometer class to determine the phone's orientation in great detail. If the acceleration vector enters a state where its largest absolute component is on the X axis, that is a landscape orientation. In theory this could be used to detect the orientation lock: if, within a few seconds of this happening, the controller does NOT receive a call to shouldRotateToInterfaceOrientation, and the [[UIDevice currentDevice] orientation] property is not in landscape, you could then safely assume the user has the orientation locked.

    This complicated, and has a delay because shouldRotateToInterfaceOrientation will be called well after the actual vector has actually entered a landscape region. The whole idea is a bit of a hack, and you should probably reconsider why you really need to present landscape views when the user has a preference not to show them.

提交回复
热议问题