您的位置:首页 > 移动开发 > IOS开发

[绍棠] 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

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS 视频
相关文章推荐