iOS手动控制屏幕旋转

版权所有,禁止匿名转载;禁止商业使用。

1.//屏幕旋转监听

-(void)addDeviceListener

{

    //用来监测屏幕旋转

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];

    _isAddDeviceListener=YES;

}

2.#pragma mark - 通知中心检测到屏幕旋转

-(void)orientationChanged:(NSNotification *)notification{

    _isHandleRotation=NO;

    [self updateOrientation];

}

- (void)updateOrientation {

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    switch (orientation) {

        case UIDeviceOrientationPortrait:

            [self toOrientation:UIInterfaceOrientationPortrait];

            break;

        case UIDeviceOrientationLandscapeLeft:

            [self toOrientation:UIInterfaceOrientationLandscapeRight];

            break;

        case UIDeviceOrientationLandscapeRight:

            [self toOrientation:UIInterfaceOrientationLandscapeLeft];

            break;

        case UIDeviceOrientationPortraitUpsideDown:

            [self toOrientation:UIInterfaceOrientationPortrait];

            break;

        default:

            break;

    }

}


3.#pragma mark - 全屏旋转处理

- (void)toOrientation:(UIInterfaceOrientation)orientation {


    if (_currentOrientation == orientation) {

        return;

    }

    _currentOrientation = orientation;

 // 手动设置状态栏方向,注意这句shouldAutoRotation为No才生效

        [[UIApplication sharedApplication] setStatusBarOrientation:_currentOrientation animated:YES];

//此处处理你要旋转的视图;

    self.showView.transform = [self getOrientation:orientation];

    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {

        

     

    }

    else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)

    {

        

    }

  

}

4.//根据状态条旋转的方向来旋转

-(CGAffineTransform)getOrientation:(UIInterfaceOrientation)orientation{

    if (orientation == UIInterfaceOrientationPortrait) {

        [self toPortraitUpdate];

        return CGAffineTransformIdentity;

    } else if (orientation == UIInterfaceOrientationLandscapeLeft){

        [self toLandscapeUpdate];

        return CGAffineTransformMakeRotation(-M_PI_2);

    } else if (orientation == UIInterfaceOrientationLandscapeRight){

        [self toLandscapeUpdate];

        return CGAffineTransformMakeRotation(M_PI_2);

    } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {

        [self toPortraitUpdate];

        return CGAffineTransformMakeRotation(M_PI);

    }

    return CGAffineTransformIdentity;

}

-(void)toLandscapeUpdate{

    _isFullScreen = YES;

    //处理状态条

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    [[UIApplication sharedApplication] setStatusBarHidden:YES];


}


-(void)toPortraitUpdate{

    _isFullScreen =NO;

    //处理状态条

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];


    [[UIApplication sharedApplication] setStatusBarHidden:NO];


}

0 0