Ответ 1
Вы можете попробовать это, может помочь вам:
Как программно изменить ориентацию устройства в iOS 6
ИЛИ
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]
Я разработал приложение iOS и протестировал его на устройстве iOS6. Во время тестирования я понял, что мое приложение не реагирует на изменения ориентации, как ожидалось.
Вот мой код:
// Autorotation (iOS >= 6.0)
- (BOOL) shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationMaskPortrait;
}
Именно поэтому я хочу знать, какие методы вызывают при изменении ориентации в iOS.
Вы можете попробовать это, может помочь вам:
Как программно изменить ориентацию устройства в iOS 6
ИЛИ
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]
Вы можете попробовать это, что решит вашу проблему.
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
См. ссылку ниже для App extension
Получить текущую ориентацию устройства (расширение приложения)
@property (nonatomic) UIDeviceOrientation m_CurrentOrientation ;
/* Вам необходимо объявить этот код в ViewDidload или ViewWillAppear */
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];
}
/* Теперь наше устройство будет предоставлять уведомление, когда мы изменим ориентацию нашего устройства. Таким образом, вы можете контролировать свой код или программу, используя текущую ориентацию */
- (void)deviceOrientationDidChange:(NSNotification *)notification
{
//Obtaining the current device orientation
/* Where self.m_CurrentOrientation is member variable in my class of type UIDeviceOrientation */
self.m_CurrentOrientation = [[UIDevice currentDevice] orientation];
// Do your Code using the current Orienation
}
Следуйте документации по UIDevice.
Вам нужно позвонить
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
Затем каждый раз, когда ориентация изменяется, вы получаете UIDeviceOrientationDidChangeNotification.
Этот учебник дает хороший и простой обзор того, как обрабатывать вращение устройства с помощью UIDeviceOrientationDidChangeNotification
. Это должно помочь вам понять, как использовать UIDeviceOrientationDidChangeNotification
для уведомления, когда ориентация устройства изменилась.
Может быть, глупо, но работает (только в ViewController):
if (self.view.frame.size.width > self.view.frame.size.height) {
NSLog(@"Hello Landscape");
}
В вашем ViewController вы можете использовать метод didRotateFromInterfaceOrientation:
для обнаружения, когда устройство было повернуто, а затем делать все, что вам нужно в каждой ориентации.
Пример:
#pragma mark - Rotation
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
switch (orientation) {
case 1:
case 2:
NSLog(@"portrait");
// your code for portrait...
break;
case 3:
case 4:
NSLog(@"landscape");
// your code for landscape...
break;
default:
NSLog(@"other");
// your code for face down or face up...
break;
}
}