1.首先项目允许旋转
WX20170506-193421.png
2.在AppDeleGate里面添加监听
// 播放器即将播放通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
// 播放器即将退出通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoFinished:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];
3.设置AppDelegate属性和方法
//AppDelegate.h
{
BOOL _isFull; // 是否全屏
}
@property (nonatomic)BOOL isFull;
//AppDelegate.m
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if(_isFull){
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
4.在需要旋转的界面
- (void)ZJVideoPlayerView_isFullScreen:(BOOL)isFullScreen{
if (isFullScreen) {
[[NSNotificationCenter defaultCenter]postNotificationName:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[self rotateScreen:isFullScreen];
self.videoPlayerView.sd_layout
.topSpaceToView(self.view, 0)
.leftSpaceToView(self.view, 0)
.rightSpaceToView(self.view, 0)
.heightIs(self.view.width / 16 * 9);
[self.videoPlayerView updateLayout];
[self.view bringSubviewToFront:self.videoPlayerView ];
}else{
[[NSNotificationCenter defaultCenter]postNotificationName:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];
[self rotateScreen:isFullScreen];
self.videoPlayerView.sd_layout
.topSpaceToView(self.view, 64)
.leftSpaceToView(self.view, 0)
.rightSpaceToView(self.view, 0)
.heightIs(self.view.width / 16 * 9);
[self.videoPlayerView updateLayout];
[self.view sendSubviewToBack:self.videoPlayerView];
}
}
//旋转界面
- (void)rotateScreen:(BOOL)isLandscapeRight
{
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
{
NSNumber *num = [[NSNumber alloc] initWithInt:(isLandscapeRight?UIInterfaceOrientationLandscapeRight:UIInterfaceOrientationPortrait)];
[[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)num];
[UIViewController attemptRotationToDeviceOrientation];
//这行代码是关键
}
SEL selector=NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation =[NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val =isLandscapeRight?UIInterfaceOrientationLandscapeRight:UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
[[UIApplication sharedApplication] setStatusBarHidden:isLandscapeRight withAnimation:UIStatusBarAnimationSlide];
}
网友评论