2016-10-29 3 views
0

アプリ内の固定デバイスの向きをポートレートのみに制限しました。しかしInstagramのように私はカメラ機能を持っていますので、現在の方向を迅速に取得する必要がありますが、向きは常にポートレートとして表示されます。ジャイロスコープを使用して手動で方向を取得する方法はありますか?アプリでオリエンテーションが制限されている場合、デバイスの向きを手動で判断するにはどうすればよいですか?

答えて

0

Swift 2.3では、次のコードを使用してAVCaptureDeviceInputの向きを設定できます(または独自の機能で置き換えることもできます)。あなたが自動回転をサポートしていない場合でも、あなたの方向性を伝えることができます> FSCameraView.swift

import CoreMotion 


//var videoInput: AVCaptureDeviceInput? 
let motionManager = CMMotionManager() 

if motionManager.gyroAvailable { 
    motionManager.deviceMotionUpdateInterval = 0.2; 
    motionManager.startDeviceMotionUpdates() 

    motionManager.gyroUpdateInterval = 1 
      motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue.currentQueue()!) { 
     (data: CMDeviceMotion?, error:NSError?) in 

     if let videoConnection = self.imageOutput?.connectionWithMediaType(AVMediaTypeVideo) { 
      if (data?.gravity.x)! > -0.6 && (data?.gravity.x)! < 0.6 { 
       if (data?.gravity.y)! < 0.4 { 
        videoConnection.videoOrientation = .Portrait 
       } else { 
        videoConnection.videoOrientation = .PortraitUpsideDown 
       } 
      } else { 
       if (data?.gravity.x)! < 0 { 
        videoConnection.videoOrientation = .LandscapeRight 
       } else { 
        videoConnection.videoOrientation = .LandscapeLeft 
       } 
      } 
     } 
     //print(data?.gravity.x) 
     //print(data?.gravity.y) 
     //print(data?.gravity.z) 
    } 
} 
1

UIDevice -

私はhttps://github.com/ytakzk/Fusumaでこのコードを使用しています。私はこれを使用して、1つのビューコントローラでのみ回転を処理します:

override func viewDidLoad() { 
     super.viewDidLoad() 
     UIDevice.current.beginGeneratingDeviceOrientationNotifications() 
     NotificationCenter.default.addObserver(self, selector: #selector(orientationChanged), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) 
    } 

    deinit { 
     UIDevice.current.endGeneratingDeviceOrientationNotifications() 
     NotificationCenter.default.removeObserver(self) 
    } 

    @objc private func orientationChanged(_ notification: Foundation.Notification) { 
     currentOrientation = UIDevice.current.orientation 
    } 
関連する問題