[绍棠] iOS播放器全屏与半屏幕按钮方法实现
2017-06-20 14:53
501 查看
/// --------- OC 播放器全屏与半屏幕按钮方法实现
- (void)toggleOrientationSwitch:(UIButton *)sender {
NSLog(@"点击的是否全屏按钮");
switch (self.playerUIState)
{
caseWBYFullScreen:
{
[UIDevicesetOrientation:UIInterfaceOrientationPortrait];
self.frame =self.oldFrame;
self.playerUIState =WBYHalfScreen;
[[UIApplicationsharedApplication]
setStatusBarStyle:UIStatusBarStyleDefault];
break;
caseWBYOut:
{
}
break;
caseWBYHalfScreen:
{
self.oldFrame =self.frame;
[UIDevicesetOrientation:UIInterfaceOrientationLandscapeRight];
self.frame =self.window.bounds;
self.playerUIState =WBYFullScreen;
[[UIApplicationsharedApplication]
setStatusBarStyle:UIStatusBarStyleLightContent];
}
break;
default:
break;
}
}
// UIDevice扩展
#import <UIKit/UIKit.h>
@interface UIDevice (WBYDevice)
+ (void)setOrientation:(UIInterfaceOrientation)orientation;
@end
#import "UIDevice+WBYDevice.h"
@implementation UIDevice (WBYDevice)
//调用私有方法实现
+ (void)setOrientation:(UIInterfaceOrientation)orientation {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[self currentDevice]];
int val = orientation;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
@end
/// Swift 播放器全屏与半屏幕按钮方法实现
extension WBYPlayerShowView {
@objcfunc toggleOrientationSwitch() {
switch playerUIState {
case .WBYFullScreen?:
print("1")
self.playerUIState = .WBYHalfScreen
toOrientation(orientation: UIInterfaceOrientation.portrait)
break
case .WBYHalfScreen?:
print("half")
self.playerUIState = .WBYFullScreen
toOrientation(orientation: UIInterfaceOrientation.landscapeRight)
break
case .WBYOut?:
print("out")
break
default:
break
}
}
/// - Parameter orientation:要旋转的方向
privatefunc toOrientation(orientation:UIInterfaceOrientation) {
//获取当前状态栏的方向
let currentOrientation = UIApplication.shared.statusBarOrientation
//如果当前的方向和要旋转的方向一致,直接return
if currentOrientation == orientation {
return
}
if orientation == UIInterfaceOrientation.portrait {
self.snp.remakeConstraints({ (make)in
make.width.equalTo(ScreenWidth)
make.left.top.right.equalTo(self.superview!).offset(0)
make.height.equalTo(ScreenWidth*0.6)
})
UIApplication.shared.setStatusBarOrientation(orientation, animated:false)
UIView.beginAnimations(nil, context:nil)
UIView.setAnimationDuration(0.35)
self.transform = CGAffineTransform.identity
self.transform = getTransformRotationAngle()
UIView.commitAnimations()
} else {
self.oldFrame =self.bounds
self.snp.remakeConstraints({ (make)in
make.width.equalTo(ScreenHeight)
make.height.equalTo(ScreenWidth)
make.center.equalTo(UIApplication.shared.keyWindow!)
})
UIApplication.shared.setStatusBarOrientation(orientation, animated:false)
UIView.beginAnimations(nil, context:nil)
UIView.setAnimationDuration(0.35)
self.transform = CGAffineTransform.identity
self.transform = getTransformRotationAngle()
UIView.commitAnimations()
}
}
/// 获取旋转角度
privatefunc getTransformRotationAngle() ->CGAffineTransform {
let interfaceOrientation = UIApplication.shared.statusBarOrientation
if interfaceOrientation == UIInterfaceOrientation.portrait {
return CGAffineTransform.identity
} elseif interfaceOrientation == UIInterfaceOrientation.landscapeLeft {
return CGAffineTransform(rotationAngle: (CGFloat)(-M_PI_2))
} elseif (interfaceOrientation == UIInterfaceOrientation.landscapeRight) {
return CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
}
return CGAffineTransform.identity
}
- (void)toggleOrientationSwitch:(UIButton *)sender {
NSLog(@"点击的是否全屏按钮");
switch (self.playerUIState)
{
caseWBYFullScreen:
{
[UIDevicesetOrientation:UIInterfaceOrientationPortrait];
self.frame =self.oldFrame;
self.playerUIState =WBYHalfScreen;
[[UIApplicationsharedApplication]
setStatusBarStyle:UIStatusBarStyleDefault];
break;
caseWBYOut:
{
}
break;
caseWBYHalfScreen:
{
self.oldFrame =self.frame;
[UIDevicesetOrientation:UIInterfaceOrientationLandscapeRight];
self.frame =self.window.bounds;
self.playerUIState =WBYFullScreen;
[[UIApplicationsharedApplication]
setStatusBarStyle:UIStatusBarStyleLightContent];
}
break;
default:
break;
}
}
// UIDevice扩展
#import <UIKit/UIKit.h>
@interface UIDevice (WBYDevice)
+ (void)setOrientation:(UIInterfaceOrientation)orientation;
@end
#import "UIDevice+WBYDevice.h"
@implementation UIDevice (WBYDevice)
//调用私有方法实现
+ (void)setOrientation:(UIInterfaceOrientation)orientation {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[self currentDevice]];
int val = orientation;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
@end
/// Swift 播放器全屏与半屏幕按钮方法实现
extension WBYPlayerShowView {
@objcfunc toggleOrientationSwitch() {
switch playerUIState {
case .WBYFullScreen?:
print("1")
self.playerUIState = .WBYHalfScreen
toOrientation(orientation: UIInterfaceOrientation.portrait)
break
case .WBYHalfScreen?:
print("half")
self.playerUIState = .WBYFullScreen
toOrientation(orientation: UIInterfaceOrientation.landscapeRight)
break
case .WBYOut?:
print("out")
break
default:
break
}
}
/// - Parameter orientation:要旋转的方向
privatefunc toOrientation(orientation:UIInterfaceOrientation) {
//获取当前状态栏的方向
let currentOrientation = UIApplication.shared.statusBarOrientation
//如果当前的方向和要旋转的方向一致,直接return
if currentOrientation == orientation {
return
}
if orientation == UIInterfaceOrientation.portrait {
self.snp.remakeConstraints({ (make)in
make.width.equalTo(ScreenWidth)
make.left.top.right.equalTo(self.superview!).offset(0)
make.height.equalTo(ScreenWidth*0.6)
})
UIApplication.shared.setStatusBarOrientation(orientation, animated:false)
UIView.beginAnimations(nil, context:nil)
UIView.setAnimationDuration(0.35)
self.transform = CGAffineTransform.identity
self.transform = getTransformRotationAngle()
UIView.commitAnimations()
} else {
self.oldFrame =self.bounds
self.snp.remakeConstraints({ (make)in
make.width.equalTo(ScreenHeight)
make.height.equalTo(ScreenWidth)
make.center.equalTo(UIApplication.shared.keyWindow!)
})
UIApplication.shared.setStatusBarOrientation(orientation, animated:false)
UIView.beginAnimations(nil, context:nil)
UIView.setAnimationDuration(0.35)
self.transform = CGAffineTransform.identity
self.transform = getTransformRotationAngle()
UIView.commitAnimations()
}
}
/// 获取旋转角度
privatefunc getTransformRotationAngle() ->CGAffineTransform {
let interfaceOrientation = UIApplication.shared.statusBarOrientation
if interfaceOrientation == UIInterfaceOrientation.portrait {
return CGAffineTransform.identity
} elseif interfaceOrientation == UIInterfaceOrientation.landscapeLeft {
return CGAffineTransform(rotationAngle: (CGFloat)(-M_PI_2))
} elseif (interfaceOrientation == UIInterfaceOrientation.landscapeRight) {
return CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
}
return CGAffineTransform.identity
}
相关文章推荐
- ios,安卓实现video小屏播放,不全屏播放的方法
- 【iOS沉思录】UIImage圆角矩形的‘离屏渲染’和‘当前屏幕渲染’实现方法
- iOS 用UIWindow实现一个悬浮按钮或者全屏覆盖效果
- IOS 中关于自定义Cell 上的按钮 开关等点击事件的实现方法(代理)
- IOS数字键盘左下角添加完成按钮的实现方法
- iOS引导登录、登录引导、login(点击某个功能按钮需要登录并且登录完成后自动跳转到要去的功能实现方法)
- [绍棠] 保持iOS设备屏幕常亮的方法
- iOS开发-UI高级 选中按钮的实现方法总结
- iOS 实现多个按钮,点选一个其它都取消选中状态的最佳方法
- iOS 实现多个按钮,点选一个其它都取消选中状态的最佳方法
- iOS实现渐变按钮Gradient Button的方法示例
- iOS使用核心动画和粒子发射器实现点赞按钮的方法
- iOS UITableView:section的Header以及Footer不悬浮停留在屏幕的方法(非在scrollView代理实现的方法)
- UIWebView中视频点击链接直接全屏播放而不是点击一个播放按钮的实现方法
- iOS实现控制屏幕常亮不变暗的方法示例
- iOS中给UITableView的侧滑删除增加多个按钮的实现方法
- iOS 利用运行时交换系统方法实现禁止同时点击两个按钮触发多个事件
- iOS实现一个可以在屏幕中自由移动的按钮
- ios实现屏幕旋转的方法
- iOS 实现多个按钮,点选一个其它都取消选中状态的最佳方法