这是我参与8月更文挑战的第7天,活动详情查看:8月更文挑战markdown
接下来咱们将作视频播放这块,而视频播放页面是横屏的,因此咱们先解决下横竖屏切换问题。app
这个枚举值用来表示物理设备的方向ide
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
来获取设备方向,在代码中监控这个方向的变化post
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.landscapeLeft
等于UIDeviceOrientation.landscapeRight
(反之亦然),这是由于向左旋转设备须要向右旋转内容spa
public enum UIInterfaceOrientation : Int {
case unknown = 0
case portrait = 1
case portraitUpsideDown = 2
case landscapeLeft = 4
case landscapeRight = 3
}
复制代码
这个和UIInterfaceOrientation有什么区别呢?是一种为了支持多种UIInterfaceOrientation而定义的debug
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 }
}
复制代码
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()
}
}
复制代码
AppDelegate
的func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
方法里面返回orientationMask
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return orientationMask
}
复制代码
3.一、在进入页面的时候设置旋转方向code
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationMask = .landscapeRight
}
复制代码
3.二、在离开页面的时候更改回来orm
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationMask = .portrait
}
}
复制代码
如今屏幕旋转搞定了,下一篇就开始作视频播放了视频