Как этого добиться в iOS?
Я имею в виду приложение DMD Panorama.
Как видите, в верхней части этого изображения есть символ Инь-Ян.
Как только мы вращаем наше устройство, два символа становятся ближе, как показано ниже:
Подскажите, пожалуйста, как я могу определить поворот устройства, чтобы при повороте устройства эти два изображения сближались?
Я ценю ваш ответ.
3 ответа
Добавьте уведомитель в функцию viewWillAppear
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];}
Изменение ориентации уведомляет эту функцию
- (void)orientationChanged:(NSNotification *)notification{
[self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];}
которая в свою очередь вызывает эту функцию, где обрабатывается кадр moviePlayerController
- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//load the portrait view
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//load the landscape view
}}
в viewDidDisappear удалить уведомление
-(void)viewDidDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];}
Сначала вы регистрируетесь для уведомления
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectDeviceOrientation) name:UIDeviceOrientationDidChangeNotification object:nil];
затем добавьте этот метод
-(void) detectDeviceOrientation
{
if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) ||
([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight))
{
// Landscape mode
} else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait)
{
// portrait mode
}
}
Попробуйте сделать следующее при загрузке приложения или при загрузке вашего представления:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
Затем добавьте следующий метод:
- (void) orientationChanged:(NSNotification *)note
{
UIDevice * device = note.object;
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
/* set frames for images */
break;
case UIDeviceOrientationPortraitUpsideDown:
/* set frames for images */
break;
default:
break;
};
}
Сказанное выше позволит вам регистрироваться для изменения ориентации устройства без включения автоматического поворота вашего обзора.