短視訊app開發,點選視訊進行全屏播放
短視訊app開發,點選視訊進行全屏播放的相關程式碼
#import "VideoFullScreenController.h" static CGFloat AnimationDuration = 0.3;//旋轉動畫執行時間 @interface VideoFullScreenController () @property (nonatomic, nullable, strong) UIView *playerView;//播放器檢視 @property (nonatomic, nullable, strong) UIButton *btnFullScreen; @property (nonatomic, nullable, strong) UIView *playerSuperView;//記錄播放器父檢視 @property (nonatomic, assign) CGRect playerFrame;//記錄播放器原始frame @property (nonatomic, assign) BOOL isFullScreen;//記錄是否全屏 @property (nonatomic, assign) UIInterfaceOrientation lastInterfaceOrientation; @property (nonatomic, nullable, strong) UIWindow *mainWindow; @end @implementation VideoFullScreenController - (void)viewDidLoad { [super viewDidLoad]; [self.playerView addSubview:self.btnFullScreen]; [self.view addSubview:self.playerView]; if (@available(iOS 13.0, *)) { _lastInterfaceOrientation = [UIApplication sharedApplication].windows.firstObject.windowScene.interfaceOrientation; } else { _lastInterfaceOrientation = [UIApplication sharedApplication].statusBarOrientation; } //開啟和監聽 裝置旋轉的通知 if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) { [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; } [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleDeviceOrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; } //裝置方向改變的處理 - (void)handleDeviceOrientationChange:(NSNotification *)notification{ UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; switch (deviceOrientation) { case UIDeviceOrientationFaceUp: NSLog(@"螢幕朝上平躺"); break; case UIDeviceOrientationFaceDown: NSLog(@"螢幕朝下平躺"); break; case UIDeviceOrientationUnknown: NSLog(@"未知方向"); break; case UIDeviceOrientationLandscapeLeft: if (self.isFullScreen) { [self interfaceOrientation:UIInterfaceOrientationLandscapeRight]; } NSLog(@"螢幕向左橫置"); break; case UIDeviceOrientationLandscapeRight: if (self.isFullScreen) { [self interfaceOrientation:UIInterfaceOrientationLandscapeLeft]; } NSLog(@"螢幕向右橫置"); break; case UIDeviceOrientationPortrait: NSLog(@"螢幕直立"); break; case UIDeviceOrientationPortraitUpsideDown: NSLog(@"螢幕直立,上下顛倒"); break; default: NSLog(@"無法辨識"); break; } } //最後在dealloc中移除通知 和結束裝置旋轉的通知 - (void)dealloc{ [[NSNotificationCenter defaultCenter]removeObserver:self]; [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications]; } - (BOOL)shouldAutorotate { return NO; } - (BOOL)prefersStatusBarHidden { if (@available(iOS 13.0, *)) { return self.isFullScreen; } return NO; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight; } #pragma mark - private method - (void)fullScreenAction:(UIButton *)sender { if (self.isFullScreen) {//如果是全屏,點選按鈕進入小屏狀態 [self changeToOriginalFrame]; } else {//不是全屏,點選按鈕進入全屏狀態 [self changeToFullScreen]; } } - (void)changeToOriginalFrame { if (!self.isFullScreen) { return; } [UIView animateWithDuration:AnimationDuration animations:^{ [self interfaceOrientation:UIInterfaceOrientationPortrait]; self.playerView.frame = self.playerFrame; } completion:^(BOOL finished) { [self.playerView removeFromSuperview]; [self.playerSuperView addSubview:self.playerView]; self.isFullScreen = NO; //呼叫以下方法後,系統會在合適的時間呼叫prefersStatusBarHidden方法,控制狀態列的顯示和隱藏,可根據自己的產品控制顯示邏輯 [self setNeedsStatusBarAppearanceUpdate]; }]; } - (void)changeToFullScreen { if (self.isFullScreen) { return; } //記錄播放器檢視的父檢視和原始frame值,在實際專案中,可能會巢狀子檢視,所以播放器的superView有可能不是self.view,所以需要記錄父檢視 self.playerSuperView = self.playerView.superview; self.playerFrame = self.playerView.frame; CGRect rectInWindow = [self.playerView convertRect:self.playerView.bounds toView:self.mainWindow]; [self.playerView removeFromSuperview]; self.playerView.frame = rectInWindow; [self.mainWindow addSubview:self.playerView]; //執行旋轉動畫 [UIView animateWithDuration:AnimationDuration animations:^{ UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; if (orientation == UIDeviceOrientationLandscapeRight) { [self interfaceOrientation:UIInterfaceOrientationLandscapeLeft]; } else { [self interfaceOrientation:UIInterfaceOrientationLandscapeRight]; } self.playerView.bounds = CGRectMake(0, 0, CGRectGetHeight(self.mainWindow.bounds), CGRectGetWidth(self.mainWindow.bounds)); self.playerView.center = CGPointMake(CGRectGetMidX(self.mainWindow.bounds), CGRectGetMidY(self.mainWindow.bounds)); } completion:^(BOOL finished) { self.isFullScreen = YES; //呼叫以下方法後,系統會在合適的時間呼叫prefersStatusBarHidden方法,控制狀態列的顯示和隱藏,可根據自己的產品控制顯示邏輯 [self setNeedsStatusBarAppearanceUpdate]; }]; } - (void)interfaceOrientation:(UIInterfaceOrientation)orientation { if (orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft) { // 設定橫屏 [self setOrientationLandscapeConstraint:orientation]; } else if (orientation == UIInterfaceOrientationPortrait) { // 設定豎屏 [self setOrientationPortraitConstraint]; } } - (void)setOrientationLandscapeConstraint:(UIInterfaceOrientation)orientation { [self toOrientation:orientation]; } - (void)setOrientationPortraitConstraint { [self toOrientation:UIInterfaceOrientationPortrait]; } - (void)toOrientation:(UIInterfaceOrientation)orientation { // 獲取到當前狀態條的方向------iOS13已經廢棄,所以不能根據狀態列的方向判斷是否旋轉,手動記錄最後一次的旋轉方向 // UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation; // 判斷如果當前方向和要旋轉的方向一致,那麼不做任何操作 if (self.lastInterfaceOrientation == orientation) { return; } if (@available(iOS 13.0, *)) { //iOS 13已經將setStatusBarOrientation廢棄,呼叫此方法無效 } else { [[UIApplication sharedApplication] setStatusBarOrientation:orientation animated:NO]; } self.lastInterfaceOrientation = orientation; // 獲取旋轉狀態條需要的時間: [UIView animateWithDuration:AnimationDuration animations:^{ // 更改了狀態條的方向,但是裝置方向UIInterfaceOrientation還是正方向的,這就要設定給你播放視訊的檢視的方向設定旋轉 // 給你的播放視訊的view檢視設定旋轉 self.playerView.transform = CGAffineTransformIdentity; self.playerView.transform = [self getTransformRotationAngleWithOrientation:self.lastInterfaceOrientation]; // 開始旋轉 } completion:^(BOOL finished) { }]; } - (CGAffineTransform)getTransformRotationAngleWithOrientation:(UIInterfaceOrientation)orientation { // 根據要進行旋轉的方向來計算旋轉的角度 if (orientation == UIInterfaceOrientationPortrait) { return CGAffineTransformIdentity; } else if (orientation == UIInterfaceOrientationLandscapeLeft){ return CGAffineTransformMakeRotation(-M_PI_2); } else if(orientation == UIInterfaceOrientationLandscapeRight){ return CGAffineTransformMakeRotation(M_PI_2); } return CGAffineTransformIdentity; } #pragma mark - setter - (void)setIsFullScreen:(BOOL)isFullScreen { _isFullScreen = isFullScreen; [self.btnFullScreen setTitle:isFullScreen?@"退出全屏":@"全屏" forState:UIControlStateNormal]; } #pragma mark - getter - (UIView *)playerView { if (!_playerView) { _playerView = [[UIView alloc]init]; _playerView.backgroundColor = [UIColor redColor]; if (@available(iOS 11.0, *)) { _playerView.frame = CGRectMake(0, self.view.safeAreaInsets.top, CGRectGetWidth(self.view.bounds), CGRectGetWidth(self.view.bounds) * 9 / 16.f); } else { _playerView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetWidth(self.view.bounds) * 9 / 16.f); } } return _playerView; } - (UIButton *)btnFullScreen { if (!_btnFullScreen) { _btnFullScreen = [UIButton buttonWithType:UIButtonTypeCustom]; [_btnFullScreen setTitle:@"全屏" forState:UIControlStateNormal]; _btnFullScreen.backgroundColor = [UIColor orangeColor]; [_btnFullScreen addTarget:self action:@selector(fullScreenAction:) forControlEvents:UIControlEventTouchUpInside]; _btnFullScreen.frame = CGRectMake(50, 80, 150, 50); } return _btnFullScreen; } - (UIWindow *)mainWindow { if (!_mainWindow) { if (@available(iOS 13.0, *)) { _mainWindow = [UIApplication sharedApplication].windows.firstObject; } else { _mainWindow = [UIApplication sharedApplication].keyWindow; } } return _mainWindow; } @end
結尾
如果你的rootViewController是UIViewController的話,那麼用上面的程式碼實現全屏效果沒有問題,如果你的rootViewController是UINavigationController或者UITabBarController的話,那麼還要增加兩個分類檔案。檔案內容如下:
一、定義分類UINavigationController+Rotation.h
@implementation UINavigationController (Rotation) - (BOOL)shouldAutorotate { return [self.topViewController shouldAutorotate]; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return [self.topViewController supportedInterfaceOrientations]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [self.topViewController preferredInterfaceOrientationForPresentation]; }
二、定義分類UITabBarController+Rotation.h
@implementation UITabBarController (Rotation) - (BOOL)shouldAutorotate { return [self.selectedViewController shouldAutorotate]; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return [self.selectedViewController supportedInterfaceOrientations]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [self.selectedViewController preferredInterfaceOrientationForPresentation]; }
以上就是 短視訊app開發,點選視訊進行全屏播放的相關程式碼,更多內容歡迎關注之後的文章
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2793983/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 短視訊app開發,內部標題進行滾動播放APP
- 短視訊app開發之ios小視訊開發經驗共享APPiOS
- 短視訊開發app,自定義帶進度條的視訊播放按鈕APP
- 短視訊app開發,長按將視訊儲存到相簿APP
- Cordova開發app——短視訊拍攝APP
- 短視訊app原始碼,點選檢視圖片,雙指放大APP原始碼
- android短視訊開發,點選靜態圖片自動跳轉播放視訊Android
- iOS開發 webview禁止視訊全屏播放iOSWebView
- 短視訊app開發,更改進度條的滑動速度APP
- 短視訊直播系統開發直播短視訊程式搭建短視訊互動直播
- 做短視訊app開發,伺服器的選擇很重要APP伺服器
- 直播短視訊原始碼短視訊APP短視訊帶貨系統多商戶直播原始碼APP
- android短視訊開發,自定義下拉選單Android
- 短視訊開發,點選按鈕Button,更換背景顏色
- 短視訊app開發,Flutter StaggeredGridView的瀑布流效果APPFlutterView
- 短視訊商城在ios短視訊開發上的應用效果iOS
- android短視訊開發,點選分享按鈕生成分享連結Android
- 短視訊技術詳解:Android端的短視訊開發技術Android
- 短視訊APP開發帶飛你的創業夢APP創業
- 短視訊開發app,webservice自定義加入攔截器APPWeb
- android短視訊開發,上傳視訊自動新增水印Android
- 短視訊app開發,點選跳轉到另一個頁面時的過場動畫APP動畫
- 被熱捧的短視訊背後,短視訊軟體開發的知識點你知道嗎?
- 短視訊app開發,視訊、直播畫面跟隨手機橫屏、豎屏移動APP
- 最快1天搭建短視訊APP!阿里雲短視訊解決方案上線APP阿里
- 短視訊seo優化,短視訊seo排名優化
- 短視訊app開發,短視訊動態功能上傳圖片時,規定圖片壓縮的大小APP
- 短視訊系統開發過程中經常被忽視的幾點
- 短視訊app開發,三種圖片並排展示的方式APP
- 短視訊app開發,顯示文字高亮,不區分大小寫APP
- 短視訊app開發,左滑刪除或長按彈出刪除選擇框APP
- 短視訊“音訊化”,音樂“視訊化”音訊
- 騰訊又出短視訊小程式“看一看短視訊”。短視訊原始碼有巨大潛力?原始碼
- 短視訊app開發,介面滑動到底的幾種實現方式APP
- 短視訊app開發,內容模糊化,解鎖更多強化功能APP
- 視訊直播app原始碼,點選圖片放大再點選縮小APP原始碼
- 短視訊平臺開發,將圖片、視訊儲存到本地的相簿中
- Android端的短視訊開發,我們該如何快速實現移動端短視訊功能?Android