美文网首页iOS开发笔记
iOS屏幕旋转:组件模块化【Swift】

iOS屏幕旋转:组件模块化【Swift】

作者: younger_times | 来源:发表于2021-08-19 11:45 被阅读0次

    关闭info.plist文件的支持方向

    也可以默认勾选第一个,如果三方不支持左右横竖屏的话,容易引起崩溃或适配问题,由开发者调整方向会好得多。


    Note:大部分说明在注释里,留意阅读

    模块化:基础VC

    设置基础VC,由子类继承

    /// 基础控件
    open class BaseVC: UIViewController{
    
        open override func viewDidLoad() {
            super.viewDidLoad()
            
            //NotificationCenter.default.addObserver(self, selector: #selector(receiverNotification), name: UIDevice.orientationDidChangeNotification, object: nil)
            
           //监听屏幕旋转的方向
           NotificationCenter.default.rx.notification(UIDevice.orientationDidChangeNotification).take(until:self.rx.deallocated).subscribe { _ in
                let orient = UIDevice.current.orientation
                var desc = ""
                switch orient {
                    case .portrait :desc = "屏幕正常竖向"
                    case .portraitUpsideDown:desc = "屏幕倒立"
                    case .landscapeLeft:desc = "屏幕左旋转"
                    case .landscapeRight:desc = "屏幕右旋转"
                    default:break
                }
                self.receiverNotification(orient: orient, desc: desc)
            }.disposed(by: LD_disposeBag)
        }
        
        /// 屏幕旋转:当设备方向改变,需要对UI进行重新布局,子类重写此方法
        @objc open func receiverNotification(orient:UIDeviceOrientation,desc:String){}
        
        //设置默认支持方向
        open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{return .portrait}
      
        /// 旋转横屏,单纯的旋转【非强制】:需要能支持横屏,即:portraitUpsideDown|landscapeLeft|landscapeRight|all
        open func forceOrientationLandscape() {
            let oriention = UIInterfaceOrientation.landscapeRight // 设置屏幕为横屏
            UIDevice.current.setValue(oriention.rawValue, forKey: "orientation")
            UIViewController.attemptRotationToDeviceOrientation()
        }
        /// 竖屏,单纯的旋转【非强制】:需要能支持竖屏,即:portraitUpsideDown|landscapeLeft|landscapeRight|all
        open func forceOrientationPortrait() {
            let oriention = UIInterfaceOrientation.portrait // 设置屏幕为竖屏
            UIDevice.current.setValue(oriention.rawValue, forKey: "orientation")
            UIViewController.attemptRotationToDeviceOrientation()
        }
    }
    
    

    屏幕旋转

    强制旋转方向是由UIDevice.current.setValue(X, forKey:"orientation")实现,前提是AppDelegatesupportedInterfaceOrientationsFor支持你需要旋转的方向,如果支持方向只允许竖屏,那么设置横屏并不会生效

    class AppDelegate: UIResponder, UIApplicationDelegate {
        ///设备支持的方向
        func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
            return .all //单纯的竖屏,然后通过 UIDevice.current.setValue 设置横屏并不生效。
        }
    }
    

    模块化:方向支持

    学习SDWebImage的方式,扩展Appdelegate,新增一个变量ld

    【此处报错】核心代码;适自己项目情况合理归类整理
    class Tool{
        public final class LDToolFisher<Base> {
            public let base: Base
            public init(_ base: Base) {self.base = base}
        }
    
        public protocol LDToolFisherCompatible {
            associatedtype CompatibleType
            var ld: CompatibleType { get }
        }
    
        public extension LDToolFisherCompatible {
            var ld: LDToolFisher<Self> {
                return LDToolFisher(self)
            }
        }
    }
    
    //==============================================
    
    【此处报错】适自己项目情况合理归类整理
    extension AppDelegate: LDToolFisherCompatible {}
    private var blockRotationDataKey: Void?
    extension LDToolFisher where Base: AppDelegate {
        /// 设置支持方向
        var blockRotation: UIInterfaceOrientationMask {
            get {return objc_getAssociatedObject(base, &blockRotationDataKey) as? UIInterfaceOrientationMask ?? .portrait}
            set {
                objc_setAssociatedObject(base, &blockRotationDataKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
                let key = "orientation"
                switch newValue {
                    case .landscapeLeft: //强制向左
                        UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey:key)
                    case .landscapeRight: //强制向右
                        UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: key)
                    case .landscape: //只允许左右
                        UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue|UIInterfaceOrientation.landscapeLeft.rawValue, forKey: key)
                    case .portrait://只允许竖
                        UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: key)
                    default:
                        //默认:左,右,竖
                        UIDevice.current.setValue(UIInterfaceOrientation.portraitUpsideDown.rawValue, forKey: key)
                }
            }
        }
    }
    
    extension AppDelegate{
        /// 支持方向
        func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
            return self.ld.blockRotation
        }
    }
    

    使用预览

    class MainVC: BaseVC{
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            【此处报错】支持方向:合理放置实际项目应有位置和简写
            (UIApplication.shared.delegate as? AppDelegate)?.ld.blockRotation = .all
            
            DispatchQueue.main.asyncAfter(deadline: .now()+2) {
            
                //2秒后横屏,可旋转回竖屏
                self.ld_forceOrientationLandscape()
                
                // 2秒后横屏【左】强制,不可再旋转回来
                //(UIApplication.shared.delegate as? AppDelegate)?.ld.blockRotation  = .landscapeLeft
            }
        }
        
        /// 屏幕变化监听,需要对UI进行重新布局
        override func receiverNotification(orient: UIDeviceOrientation, desc: String) {
            print(orient)
            print(desc)
        }    
    }
    

    相关文章

      网友评论

        本文标题:iOS屏幕旋转:组件模块化【Swift】

        本文链接:https://www.haomeiwen.com/subject/vgiobltx.html