ios - 如何在 Objective-C 中不旋转的视图控制器中检测设备的真实方向?
问题描述
我有一个总是纵向的视图控制器。有一个特定的功能根据设备本身的当前方向工作,而不是视图控制器的方向。
在这种情况下,如何检测设备的方向并在方向改变时触发功能?
解决方案
自 iOS 8 以来,他们介绍viewWillTransitionToSize
了每次旋转发生时都会触发
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
// do what you need with orientation here
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
}];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
推荐阅读
- javascript - 为什么当我将 gas 增加到大于 10 时我的合约购买失败?
- java - 测试适用于 Java 的 Azure 权限 SDK 时出现 NoClassDefFoundError
- python - TypeError:预期的字符串或类似字节的对象我收到此错误
- python - 将实时视频帧从用户/客户端(Android 应用程序、iPhone 应用程序、python)流式传输到 Flask API(服务器),然后返回到客户端
- android-studio - 如何在 Android Studio 中增加数据库检查器查询部分的字体大小
- wordpress - 基于可用变体的 WordPress WooCommerce get_sign_up_fee
- django - 如何在模板中动态引用 Django 查询集对象中的字段?
- python - 我应该对哪些数据进行标准化以训练一个好的模型?
- c# - 从datagridview c#中获取一个特定的单元格
- cakephp - Cakephp 3 动态图表显示