肖像和风景 - 在模拟器中工作,但不在设备上

时间:2014-04-29 11:58:26

标签: ios uiviewcontroller

我一直在努力寻找解决方案,因此我有纵向和横向模式的自定义设计。

一切都在模拟器中工作,但我不确定我是否已正确完成工作。

我已经设置了两个视图控制器,一个用于纵向(ViewController),另一个用于横向(landScapeViewController)。

ViewController.h我添加了:

@property (readwrite, assign) BOOL isShowingLandscapeView;
@property (readwrite, assign) BOOL previousOrientation;

ViewController.m我已添加:

-(void)awakeFromNib
{
    _isShowingLandscapeView = NO;
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                       selector:@selector(orientationChanged:)           name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
        !_isShowingLandscapeView)
    {
        [self performSegueWithIdentifier:@"DisplayAlternateView" sender:self];
        _isShowingLandscapeView = YES;

   }
    else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
             _isShowingLandscapeView)

    {
            [self dismissViewControllerAnimated:YES completion:nil];
            _isShowingLandscapeView = NO;
        }
    }

在此之后我修改了两个ViewControllers。

有人可以告诉我,如果我做得对吗?

1 个答案:

答案 0 :(得分:0)

使用以下方式,您在注册通知时出错:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationIsChanged) name:UIDeviceOrientationDidChangeNotification object:nil]; 

- (void) orientationIsChanged 
{
    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
        ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight))
         {
                // Do your stuff for LandScape  orientation
    } 
    else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) 
    {
        // Do your stuff for Portrait orientation
    }   
}

或只需检测如下:

if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) 
{
        // Do your stuff for LandScape  orientation
} 
else if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) 
{
        // Do your stuff for Portrait orientation
}