OC-related screen rotation

OC rotating screen divided into two parts, the first is to open the Device Orientation, then opened their own rotation, no need to manually open process. Because most are automatic layout, usually with less than this, look at the recent AVFoundation related stuff, need to use this, so summarize

 

 

 The first part, open the automatic rotation:

  • (1) notify the registration screen rotation:
 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    //注册屏幕旋转通知
 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientChange:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:[UIDevice currentDevice]];
- (void)orientChange:(NSNotification *)notification{
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    if(orientation == UIDeviceOrientationPortrait){
         NSLog(@"home在下方");
        self.deviceOrientationBtn.selected = NO;
    }else if(orientation == UIDeviceOrientationPortraitUpsideDown){
         NSLog(@"home在上方");
        self.deviceOrientationBtn.selected = NO;
    }else if(orientation == UIDeviceOrientationLandscapeLeft){
         NSLog(@"home在右方");
        self.deviceOrientationBtn.selected = YES;
    }else if(orientation == UIDeviceOrientationLandscapeRight){
         NSLog(@"home在左方");
        self.deviceOrientationBtn.selected = YES;
    }

}

 If I want to manually after turning horizontal screen or vertical screen, you can also use the code enforced:

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight]

 or

 [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"]

 If you want to enter or change ViewController on horizontal screen vertical screen, then resume after the withdrawal, you can

-(void)viewWillAppear:(BOOL)animated

 and

-(void)viewWillDisappear:(BOOL)animated:

 Two methods to achieve

 

 

The second part, do not open the screen rotation, you need to manually do a fake instead

(1) Method horizontal screen settings:

- (void)fullScreenWithDirection:(UIInterfaceOrientation)direction{
    if (direction == UIInterfaceOrientationLandscapeLeft){
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
        [UIView animateWithDuration:0.25 animations:^{
            self.view.transform = CGAffineTransformMakeRotation(M_PI / 2);
        }completion:^(BOOL finished) {
//            [self setFullStatusBarHiddenType:_fullStatusBarHiddenType];
        }];
    }else if (direction == UIInterfaceOrientationLandscapeRight) {
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
        [UIView animateWithDuration:0.25 animations:^{
            self.view.transform = CGAffineTransformMakeRotation(0);
        }completion:^(BOOL finished) {
//            [self setFullStatusBarHiddenType:_fullStatusBarHiddenType];
        }];
    }
    self.view.frame = [[UIApplication sharedApplication]keyWindow].bounds;
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
    
}

 

(2) reduction portrait

[UIView animateWithDuration:0.25 animations:^{
            self.view.transform = CGAffineTransformMakeRotation(0);
            self.view.frame = [[UIApplication sharedApplication]keyWindow].bounds;
            [self.view setNeedsLayout];
            [self.view layoutIfNeeded];
        }completion:^(BOOL finished) {
        }];

 

Guess you like

Origin www.cnblogs.com/hualuoshuijia/p/11548111.html