转自 http://blog.duicode.com/1868.html
最近做的项目中涉及到全屏播放问题,但这个项目都是竖屏模式的,只有全屏播放视频的时候才是横屏,考虑的方案是将视频的view旋转90°,但是会出现一个问题,那就是状态栏并不是横屏显示的,刚开始我将它隐藏掉了,可是会出现黑边,而且旋转效果也不合适,经查找发现可以将状态栏强制设置为横屏的,虽然该方式已经标记过时了,但是还是可以使用的,而且这样的旋转动画也是比较顺畅的,下面来看代码:
- (void)fullView{
self.videoView.videoViewParentView = self.videoView.superview;
self.videoView.videoViewFrame = self.videoView.frame;
self.fullButton.hidden = YES;
self.backButton.hidden = NO;
CGRect rectInWindow = [self.videoView convertRect:self.videoView.bounds toView:kAPP_Window];
self.videoView.frame = rectInWindow;
[kAPP_Window addSubview:self.videoView];
[UIView animateWithDuration:0.25 animations:^{
self.videoView.transform = CGAffineTransformMakeRotation(M_PI_2);
self.videoView.bounds = CGRectMake(0, 0, CGRectGetHeight(self.videoView.superview.bounds), CGRectGetWidth(self.videoView.superview.bounds));
self.videoView.center = CGPointMake(CGRectGetMidX(self.videoView.superview.bounds), CGRectGetMidY(self.videoView.superview.bounds));
} completion:^(BOOL finished) {
self.activityIndicator.center = self.videoView.center;
}];
[self refreshStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
}
- (void)backView{
self.fullButton.hidden = NO;
self.backButton.hidden = YES;
CGRect frame = [self.videoView.videoViewParentView convertRect:self.videoView.videoViewFrame toView:[UIApplication sharedApplication].keyWindow];
[UIView animateWithDuration:0.25 animations:^{
self.videoView.transform = CGAffineTransformIdentity;
self.videoView.frame = frame;
} completion:^(BOOL finished) {
[self.videoView removeFromSuperview];
self.videoView.frame = self.videoView.videoViewFrame;
[self.videoView.videoViewParentView addSubview:self.videoView];
self.activityIndicator.center = self.videoView.center;
}];
[self refreshStatusBarOrientation:UIInterfaceOrientationPortrait];
}
- (BOOL)shouldAutorotate {
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (void)refreshStatusBarOrientation:(UIInterfaceOrientation)interfaceOrientation {
[[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:YES];
}