前言
接下来我们将做视频播放这块,而视频播放页面是横屏的,所以我们先解决下横竖屏切换问题。
我们先来看下关于方向的两个枚举和一个结构体:
UIDeviceOrientation:设备方向
这个枚举值用来表示物理设备的方向
public enum UIDeviceOrientation : Int {
case unknown = 0
case portrait = 1 // Device oriented vertically, home button on the bottom
case portraitUpsideDown = 2 // Device oriented vertically, home button on the top
case landscapeLeft = 3 // Device oriented horizontally, home button on the right
case landscapeRight = 4 // Device oriented horizontally, home button on the left
case faceUp = 5 // Device oriented flat, face up
case faceDown = 6 // Device oriented flat, face down
}
这个只能读取,不能去设置,我们可以使用
UIDevice.current.orientation来获取设备方向,在代码中监控这个方向的变化
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientation), name: UIDevice.orientationDidChangeNotification, object: nil)
return true
}
@objc private func deviceOrientation() {
switch UIDevice.current.orientation {
case .portrait:
debugPrint("portrait")
case .portraitUpsideDown:
debugPrint("portraitUpsideDown")
case .landscapeLeft:
debugPrint("landscapeLeft")
case .landscapeRight:
debugPrint("landscapeRight")
case .faceUp:
debugPrint("faceUp")
case .faceDown:
debugPrint("faceDown")
default:
debugPrint("unknown")
}
}
UIInterfaceOrientation:页面方向
这个是可以设置的,注意:
UIInterfaceOrientation.landscapeLeft等于UIDeviceOrientation.landscapeRight(反之亦然),这是因为向左旋转设备需要向右旋转内容
public enum UIInterfaceOrientation : Int {
case unknown = 0
case portrait = 1
case portraitUpsideDown = 2
case landscapeLeft = 4
case landscapeRight = 3
}
UIInterfaceOrientationMask:页面方向
这个和UIInterfaceOrientation有什么区别呢?是一种为了支持多种UIInterfaceOrientation而定义的
public struct UIInterfaceOrientationMask : OptionSet {
public init(rawValue: UInt)
public static var portrait: UIInterfaceOrientationMask { get }
public static var landscapeLeft: UIInterfaceOrientationMask { get }
public static var landscapeRight: UIInterfaceOrientationMask { get }
public static var portraitUpsideDown: UIInterfaceOrientationMask { get }
public static var landscape: UIInterfaceOrientationMask { get }
public static var all: UIInterfaceOrientationMask { get }
public static var allButUpsideDown: UIInterfaceOrientationMask { get }
}
设置页面横竖屏
1、AppDelegate里面增加一个orientationMask属性
//记录当前界面横竖屏旋转方向
var orientationMask: UIInterfaceOrientationMask = .portrait {
didSet {
if orientationMask.contains(.portrait){
//强制设置成竖屏
UIDevice.current.setValue(NSNumber(value: UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
} else {
//强制设置成横屏UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
}
/// 刷新,如果不设置的话,当在横屏状态下锁屏,然后在解锁,在返回到上一页会发现屏幕没有更改过来
UIViewController.attemptRotationToDeviceOrientation()
}
}
2、在AppDelegate的func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask方法里面返回orientationMask
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return orientationMask
}
3、在需要的设置屏幕旋转的控制器里面设置旋转方向
3.1、在进入页面的时候设置旋转方向
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationMask = .landscapeRight
}
3.2、在离开页面的时候更改回来
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationMask = .portrait
}
}
现在屏幕旋转搞定了,下一篇就开始做视频播放了