仿QQ视频全屏界面旋转实现

Posted 妖妖yaoyao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了仿QQ视频全屏界面旋转实现相关的知识,希望对你有一定的参考价值。

// 实现效果:界面支持系统自动转屏和点击转屏
//  ViewController.m
//  rotatoTest

//  Copyright © 2016年 yaoyao. All rights reserved.
//  1.关闭系统自动转屏
//  2.获取设备方向,设置转屏,
//  3.点击按钮,设置转屏

#import "ViewController.h"
#import <UIKit/UIKit.h>
#import <CoreGraphics/CoreGraphics.h>

@interface ViewController ()
{
    float width;
    float height;

}
@property (nonatomic,retain)UIImageView *imageView;


@end

@implementation ViewController


- (void)viewDidLoad {
 
    
    /**
     * UIImage 对象
     */
    UIImage *image = [UIImage imageNamed:@"000.jpg"];
    self.imageView.image = image;
    
    // 设置图片范围
    CGFloat imageH = image.size.height;
    CGFloat imageW = image.size.width;
    CGFloat imageX = 0;
    CGFloat imageY = 0;
    self.imageView.frame = CGRectMake(imageX, imageY, width, 100);
    self.imageView.userInteractionEnabled = YES;
    [self.view addSubview:self.imageView];
    
    
    //相当于qq视频上面的全屏按钮
    UIButton *btnL = [UIButton buttonWithType:UIButtonTypeCustom];
    btnL.frame = CGRectMake(20, 20,150, 20);
    [btnL setTitle:@"点击向左横屏" forState:UIControlStateNormal];
    btnL.backgroundColor = [UIColor blueColor];
    [btnL addTarget:self action:@selector(btnLClick) forControlEvents:UIControlEventTouchUpInside];
    [self.imageView addSubview:btnL];
    
    //相当于qq视频全屏界面上的返回按钮
    UIButton *btnP = [UIButton buttonWithType:UIButtonTypeCustom];
    btnP.frame = CGRectMake(200, 20,100, 20);
    [btnP setTitle:@"点击竖屏" forState:UIControlStateNormal];
    btnP.backgroundColor = [UIColor blueColor];
    [btnP addTarget:self action:@selector(btnPClick) forControlEvents:UIControlEventTouchUpInside];
    [self.imageView addSubview:btnP];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

//支持旋转(当手动转屏时关闭自动转屏)
-(BOOL)shouldAutorotate
{
    return NO;
}


//支持的方向(亲测:当上个方法返回no时,这个方法依然调用)
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    NSLog(@"supportedInterfaceOrientations调用了");
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
    
}

-(void)btnLClick
{
    [[UIApplication sharedApplication]setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:YES];
    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:duration];
    //在这里设置view.transform需要匹配的旋转角度的大小就可以了。
    self.imageView.transform = CGAffineTransformIdentity;
    self.imageView.transform = CGAffineTransformMakeRotation(M_PI/2.0);
    self.imageView.frame = CGRectMake(0, 0, width, height);
    [UIView commitAnimations];

}

//点击返回竖屏
-(void)btnPClick
{
    [[UIApplication sharedApplication]setStatusBarOrientation:UIDeviceOrientationPortrait   animated:YES];
    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:duration];
    //在这里设置view.transform需要匹配的旋转角度的大小就可以了。
    self.imageView.transform = CGAffineTransformIdentity;
    self.imageView.transform = CGAffineTransformMakeRotation(0);
    self.imageView.frame = CGRectMake(0, 0, width, 100);
    [UIView commitAnimations];
    
}



- (void)handleDeviceOrientationDidChange:(UIInterfaceOrientation)interfaceOrientation
{
    //1.获取 当前设备 实例
    UIDevice *device = [UIDevice currentDevice] ;
    
    /**
     *  2.取得当前Device的方向,Device的方向类型为Integer
     *
     *  必须调用beginGeneratingDeviceOrientationNotifications方法后,此orientation属性才有效,否则一直是0。orientation用于判断设备的朝向,与应用UI方向无关
     *
     *  @param device.orientation
     *
     */
    
    switch (device.orientation) {
        case UIDeviceOrientationFaceUp:
        {
            NSLog(@"屏幕朝上平躺");
            
        }
            
            break;
            
        case UIDeviceOrientationFaceDown:
            NSLog(@"屏幕朝下平躺");
            break;
            
            //系統無法判斷目前Device的方向,有可能是斜置
        case UIDeviceOrientationUnknown:
            NSLog(@"未知方向");
            break;
            
        case UIDeviceOrientationLandscapeLeft:
        {
            NSLog(@"屏幕向左横置");
            [[UIApplication sharedApplication]setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:YES];
            CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:duration];
            //在这里设置view.transform需要匹配的旋转角度的大小就可以了。
            self.imageView.transform = CGAffineTransformIdentity;
            self.imageView.transform = CGAffineTransformMakeRotation(M_PI/2.0);
            self.imageView.frame = CGRectMake(0, 0, width, height);
            [UIView commitAnimations];
            
        }
            
            break;
            
        case UIDeviceOrientationLandscapeRight:
        {
            NSLog(@"屏幕向右橫置");
            [[UIApplication sharedApplication]setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:YES];
            CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:duration];
            //在这里设置view.transform需要匹配的旋转角度的大小就可以了。
            self.imageView.transform = CGAffineTransformIdentity;
            self.imageView.transform = CGAffineTransformMakeRotation(-M_PI/2.0);
            self.imageView.frame = CGRectMake(0, 0, width, height);
            [UIView commitAnimations];
        }
            break;
            
        case UIDeviceOrientationPortrait:
        {
            NSLog(@"屏幕直立");
            [[UIApplication sharedApplication]setStatusBarOrientation:UIDeviceOrientationPortrait animated:YES];
            CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:duration];
            //在这里设置view.transform需要匹配的旋转角度的大小就可以了。
            self.imageView.transform = CGAffineTransformIdentity;
            self.imageView.transform = CGAffineTransformMakeRotation(0);
            self.imageView.frame = CGRectMake(0, 0, width, 100);
            [UIView commitAnimations];
            
        }
            break;
            
        case UIDeviceOrientationPortraitUpsideDown:
            NSLog(@"屏幕直立,上下顛倒");
            break;
            
        default:
            NSLog(@"无法辨识");
            break;
    }
    
}


-(void)viewDidAppear:(BOOL)animated
{
    
    /**
     *  开始生成 设备旋转 通知
     */
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    
    
    /**
     *  添加 设备旋转 通知
     *
     *  @param handleDeviceOrientationDidChange: handleDeviceOrientationDidChange: description
     *
     *  @return return value description
     */
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleDeviceOrientationDidChange:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil
     ];
    
}



-(void)viewDidDisappear:(BOOL)animated
{
    /**
     *  销毁 设备旋转 通知
     *
     *  @return return value description
     */
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIDeviceOrientationDidChangeNotification
                                                  object:nil
     ];
    
    
    /**
     *  结束 设备旋转通知
     *
     *  @return return value description
     */
    [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma 懒加载

- (UIImageView *)imageView
{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
    }
    return _imageView;
}



@end

 

以上是关于仿QQ视频全屏界面旋转实现的主要内容,如果未能解决你的问题,请参考以下文章

Angdroid Studio仿QQ界面实现简单的功能

仿QQ好友列表界面的实现

Android仿QQ界面

swift 3 - 旋转到横向时全屏播放视频

JavaFX自定义窗口标题栏

微信看视频不能全屏怎么办