iOS device orientation disregarding orientation lock

后端 未结 10 1273
天涯浪人
天涯浪人 2020-11-29 01:59

I want to query the orientation the iPhone is currently in. Using

[UIDevice currentDevice].orientation

works as long as the device isn\'t

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 02:45

    my solution using coremotion,it work even when the device has his orientation locked.

        let motionManager: CMMotionManager = CMMotionManager()
    

    on the did load method

    motionManager.deviceMotionUpdateInterval = 0.01
        if motionManager.accelerometerAvailable{
            let queue = NSOperationQueue()
            motionManager.startAccelerometerUpdatesToQueue(queue, withHandler:
                {data, error in
    
                    guard let data = data else{
                        return
                    }
                    let angle = (atan2(data.acceleration.y,data.acceleration.x))*180/M_PI;
    
                    print(angle)
                    if(fabs(angle)<=45){
                        self.orientation = AVCaptureVideoOrientation.LandscapeLeft
                        print("landscape left")
                    }else if((fabs(angle)>45)&&(fabs(angle)<135)){
    
                        if(angle>0){
                            self.orientation = AVCaptureVideoOrientation.PortraitUpsideDown
                            print("portrait upside Down")
    
    
                        }else{
                            self.orientation = AVCaptureVideoOrientation.Portrait
                            print("portrait")
    
                        }
                    }else{
                        self.orientation = AVCaptureVideoOrientation.LandscapeRight
                        print("landscape right")
    
                    }
    
    
                }
            )
        } else {
            print("Accelerometer is not available")
        }
    

    hope it helps.

提交回复
热议问题