Device tilting up/down and sideways triggers orientation notification

倾然丶 夕夏残阳落幕 提交于 2019-12-02 07:17:29

(a more complete answer than my comment)

When handling view controller rotations, you can't use the device rotation from [UIDevice currentDevice].orientation -- that gives you the instantaneous orientation of the device. It doesn't necessarily match the interface orientation, and can change back and forth rapidly.

The interface orientation is handled by the responder chain. UIViewControllers receive messages when it changes. These messages happen when the orientation has stabilized, and will not fluctuate rapidly.

To handle rotation:

  • First, in iOS 8 docs, look at "Handling View Rotations" in UIViewController

  • Pre-iOS8, override didRotateFromInterfaceOrientation in your ViewController. Your view controller receives this when the device rotates. You handle the rotation at that time.

  • Starting in iOS8, use the Transition Coordinator mechanism. Set your transition coordinator using the transitionCoordinator property of the view controller.

Or, you can avoid handling rotations altogether if you've set up your autolayout constraints completely. See (http://annabd351.github.io/SquareCropViewController) for a good example of a view controller which handles some tricky rotation scenarios with almost no code.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!