Search code examples
iosswiftdevice-orientation

Fix Orientation and list for changes - SWIFT


I'm new to iOS development and struggling with orientation checks. I have fixed the orientation for my view and now want to see the orientation it's held in.

How I've fixed it:

APP DELEGATE

        return self.orientationLock
    }
    struct AppUtility {
        static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
            if let delegate = UIApplication.shared.delegate as? AppDelegate {
                delegate.orientationLock = orientation
            }
        }
            
        static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo 
                                      rotateOrientation:UIInterfaceOrientation) {
            self.lockOrientation(orientation)
            UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
        }
    } 

VIEW CONTROLLER

override func viewWillAppear(_ animated: Bool) {
        AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait, 
                                               andRotateTo: UIInterfaceOrientation.portrait)
    }

Now I want to listen for the orientation and print for the moment the changes.

VIEW CONTROLLER

override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
        switch UIDevice.current.orientation {
        case .portrait:
            print("portrait")
        case .faceUp:
            print("faceUp")
        case .landscapeLeft:
            print("landscape left")
        case .landscapeRight:
            print("landscape right")
        case .portraitUpsideDown:
            print("upsidedown portrait")
        default:
            print("something else")
            
        }
    }

The trouble is the above code is waiting for the view to change, the view has been locked, so it'll never change.

How would you go about locking the view and then listening to see how the phone is held?

Much Appreciated


Solution

  • Shawn, thanks for the advice. In the end, that's what I did. Here's my code

    You'll also need to import CoreMotion

        var motion = CMMotionManager()
        var currentOrientation: String = "Portrait"
        
        func oritentationUpdateStart() {
            motion.accelerometerUpdateInterval = 1.0
            motion.startAccelerometerUpdates(to: OperationQueue.current! ) { (data, error) in
                if let accel = data {
                    if (accel.acceleration.x > 0.5){
                        self.currentOrientation = "Upsidedown Landscape"
                    }
                    else if (accel.acceleration.y > 0.5) {
                        self.currentOrientation = "Upsidedown Portrait"
                    }
                    else if (accel.acceleration.x < -0.5) {
                        self.currentOrientation = "Landscape"
                    }
                    else if (accel.acceleration.y < -0.5) {
                        self.currentOrientation = "Portrait"
                    }
                }
            }
        }