百度搜“ios开发手机竖着如何横屏播放”的时候只能搜到 强制旋转屏幕 或 需要手动旋转手机 的答案, 都不是想要的。
然后在 腾讯视频播放sdk的demo中发现有旋转播放的源码
下载地址 https://cloud.tencent.com/document/product/881
在SuperPlayerView.m
/** 全屏 */
- (void)setFullScreen:(BOOL)fullScreen {
if (_isFullScreen != fullScreen) {
//旋转
[self _adjustTransform:[self _orientationForFullScreen:fullScreen]];
//中间黑方块
[self _switchToFullScreen:fullScreen];
//其他比如状态栏
[self _switchToLayoutStyle:fullScreen ? SuperPlayerLayoutStyleFullScreen : SuperPlayerLayoutStyleCompact];
}
_isFullScreen = fullScreen;
}
- (void)_adjustTransform:(UIDeviceOrientation)orientation {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
self.transform = [self getTransformRotationAngleOfOrientation:orientation];
_fullScreenBlackView.transform = self.transform;
[UIView commitAnimations];
}
上面这个方法执行了一下动画
下面这个是旋转的核心代码
/**
* 获取变换的旋转角度
*
* @return 变换矩阵
*/
- (CGAffineTransform)getTransformRotationAngleOfOrientation:(UIDeviceOrientation)orientation {
// 状态条的方向已经设置过,所以这个就是你想要旋转的方向
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if (interfaceOrientation == (UIInterfaceOrientation)orientation) {
return CGAffineTransformIdentity;
}
// 根据要进行旋转的方向来计算旋转的角度
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;
}