凌雪
2018-11-01
来源 :网络
阅读 1512
评论 0
摘要:本文将带你了解IOS开发入门iOS视频播放横竖屏切换技巧,希望本文对大家学IOS有所帮助。
本文将带你了解IOS开发入门iOS视频播放横竖屏切换技巧,希望本文对大家学IOS有所帮助。
iOS视频播放横竖屏切换技巧。
一、需求:横竖屏切换。
二、效果:
三、实现:
如上图,点击工具栏的第四个按钮进行横屏切换:
- (void)toolTabButtonPressed:(AKVideoToolTab1 *)sender{
if (sender.selectedIndex == 4) {//切换至全屏
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appdelegate.allowRotation = YES;
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight]; [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}}
假如在项目配置的时候整个项目是仅支持竖屏,所以在需要横屏的时候就需要通过代码进行控制:
appdelegate.allowRotation = YES;//允许横屏,当然退出本界面的时候要记得将其设为NO。
然后,设置横屏效果,才会生效:
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
至此,还没真正看得到横屏的效果,需对横屏变化进行监听,然后对UI进行变换。
四、横竖屏通知监听:
iOS横竖屏通知有两种,一种设备横竖屏状态变化,另一种状态栏横竖屏状态变化。与UI布局相关的是后者,因为如果一个viewController不支持自动旋转,并且相关view也没有进行横竖屏切换控制,而当设备由竖屏转横屏时依然会执行监听设备横竖屏的通知方法。以下对两种方式的简述:
1.监听设备横竖屏(UIDeviceOrientationDidChangeNotification)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
4种状态:
UIDeviceOrientationPortrait
UIDeviceOrientationPortraitUpsideDown
UIDeviceOrientationLandscapeLeft
UIDeviceOrientationLandscapeRight
2.监听状态栏横竖屏状态(UIApplicationDidChangeStatusBarOrientationNotification)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
4种状态:
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
要实现上图效果就要监听状态栏横竖屏状态:
#pragma mark ------------orientChangeNotification 监听横竖屏切换--------------
- (void)orientChange:(NSNotification *)notification{
UIInterfaceOrientation interfaceOritation = [[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOritation == UIInterfaceOrientationLandscapeRight) {
videoView.frame = CGRectMake(0 , 0, kScreenWidth, kScreenheight);
self.navigationController.navigationBarHidden = YES;
CGFloat buttonW = 44;
exitFullScreen = ({
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(30, 10, buttonW, buttonW);
[button setImage:[UIImage imageNamed:@"全屏播放-返回"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(exitFullScreen) forControlEvents:UIControlEventTouchUpInside];
button;
});
[self.view addSubview:exitFullScreen];
}else if(interfaceOritation == UIInterfaceOrientationPortrait){
CGFloat verticalH = (kScreenWidth*0.6);
videoView.frame = CGRectMake(0, 60, kScreenWidth, verticalH);
self.navigationController.navigationBarHidden = NO;
[exitFullScreen removeFromSuperview];
}
}
当监听到是横屏UIInterfaceOrientationLandscapeRight的时候就进行横屏的UI布局。当是向竖屏UIInterfaceOrientationPortrait改变时进行竖屏的UI回归。效果如上。
ViewController的代码如下:
//
// LHTViewController.m
// CACloud
//
// Created by Sure on 2017/8/24.
// Copyright ? 2017年 Sure. All rights reserved.
//
#import "LHViewController.h"
#import "AKVideoToolTab1.h"
@interface LHViewController ()<uigesturerecognizerdelegate,akvideotooltab1delegate>{
UIImageView *videoView;
UIButton *exitFullScreen;
}
@end
@implementation LHViewController{
AKVideoToolTab1 *akTab;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
[self addUI];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] setIdleTimerDisabled:NO] ;//关闭屏幕常亮;
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appdelegate.allowRotation = NO;
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
[super viewWillDisappear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)addUI{
CGFloat verticalH = (kScreenWidth*0.6);
videoView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, kScreenWidth, verticalH)];
videoView.backgroundColor = [UIColor blackColor];
[self.view addSubview:videoView];
CGFloat toolH = 30;
akTab = [[AKVideoToolTab1 alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(videoView.frame), kScreenWidth,toolH)];
[self.view addSubview:akTab];
CGFloat BtnW = 100;
CGFloat gap = (kScreenWidth - 3*100)/4;
}
#pragma mark ------------AKVideoToolTab1:播放、声音、清晰度、全屏--------------
- (void)toolTabButtonPressed:(AKVideoToolTab1 *)sender{
if (sender.selectedIndex == 4) {//切换至全屏
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appdelegate.allowRotation = YES;
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
}
#pragma mark ------------orientChangeNotification 监听横竖屏切换--------------
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号