This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging

preface

Now, we’re going to do the video, and the video is landscape, so let’s do the portrait switch first.

Let’s start with two enumerations and a structure about direction:

UIDeviceOrientation: Device orientation

This enumeration value is used to indicate the direction of the physical device

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
}
Copy the code

This can only be read, can’t go to set, we can use the UIDevice. Current. The orientation direction to obtain equipment, monitoring the change of the direction in the code

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")
    }
}
Copy the code

UIInterfaceOrientation: Page orientation

This can be set up, note: UIInterfaceOrientation. LandscapeLeft equals UIDeviceOrientation. LandscapeRight (and vice versa), this is because the rotating equipment need to rotate the content to the right to the left

public enum UIInterfaceOrientation : Int {
    case unknown = 0
    case portrait = 1
    case portraitUpsideDown = 2
    case landscapeLeft = 4
    case landscapeRight = 3
}
Copy the code

UIInterfaceOrientationMask: page orientation

How is this different from UIInterfaceOrientation? It’s one that’s defined to support multiple 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 }
}
Copy the code

Set the horizontal and vertical screen

1.AppDelegateAdd one insideorientationMaskattribute

// Record the rotation direction of the current screen UIInterfaceOrientationMask =. Portrait {didSet {if orientationMask. The contains (. Portrait) {/ / set to portrait UIDevice.current.setValue(NSNumber(value: UIInterfaceOrientation.portrait.rawValue), forKey: "Orientation")} else {/ / compulsory set into a landscape UIDevice. Current. The setValue (UIInterfaceOrientation. LandscapeRight. RawValue, forKey: "Orientation ")} In no change to return to the previous page will find screen over UIViewController. AttemptRotationToDeviceOrientation ()}}Copy the code

2, inAppDelegatethefunc application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMaskMethod to returnorientationMask

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return orientationMask
}
Copy the code

3. Set the rotation direction in the controller where you need to set the screen rotation

3.1. Set the rotation direction when entering the page

if let delegate = UIApplication.shared.delegate as? AppDelegate {
    delegate.orientationMask = .landscapeRight
}
Copy the code

Change it back when you leave the page

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    if let delegate = UIApplication.shared.delegate as? AppDelegate {
        delegate.orientationMask = .portrait
    }
}
Copy the code

Now that the screen is rotated, it’s time to start the next video