##質問内容
iPhoneにはpitch,roll,yawが姿勢センサの軸として定義されていますが、それらはiPhoneの画面の向きによって変化するのでしょうか。
##詳細
XcodeでCoreMotionを実装し、roll,pitch,yawの値をラベルに表示させるアプリを作成しました。設定画面のGeneral > Deployment Info > Device Orientationから画面のどの向きにアプリを表示するか選択できるので、私はDevice Orientationの値をPortraitからLandscape Leftに設定しました。すると、アプリのUIが横画面になった途端3軸の方向が以下の画像のように変化しました。(画面を色々な方向に傾けて測定しました。)
アプリの表示方向によらずに「rollはアプリの下端から上端方向をむく」といった定義をした方が、画面の向きによってpitch,roll,yawの受け取る値の処理方法を変更しなくて済むためより良い方法かと思われるのですが、なぜそのようになっていないのでしょうか。
そもそも私の3軸の測定が間違えているのか、もし測定が正しいならなぜそのような仕様になっているのかを教えていただければと思います。
Xcodeのバージョンは8.3.2,iPhoneはiOS10.3,swiftのバージョンは3.1です。
以下に測定に用いたコードを載せます。
swift
1import UIKit 2import CoreMotion 3 4class nextPageViewController: UIViewController { 5 @IBOutlet weak var yawLabel: UILabel! 6 @IBOutlet weak var pitchLabel: UILabel! 7 @IBOutlet weak var rollLabel: UILabel! 8 9//coreMotionマネージャを作る 10 let cmManager = CMMotionManager() 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 //キューの実行間隔 14 cmManager.deviceMotionUpdateInterval=0.1 15 //キューで実行するクロージャを定義 16 let handler:CMDeviceMotionHandler = 17 {(motionData:CMDeviceMotion?,error:Error?)->Void in self.motionAnimation(motionData,error)} 18 //更新で実行するキューを登録してモーションセンサをスタート 19 cmManager.startDeviceMotionUpdates(to: OperationQueue.main, withHandler: handler) 20 } 21 func motionAnimation(_ motionData:CMDeviceMotion?,_ error:Error?){ 22 if let motion = motionData{ 23 var yaw = motion.attitude.yaw 24 yaw = round(yaw*100)/100 25 yawLabel.text = String(yaw) 26 var pitch = motion.attitude.pitch 27 pitch = round(pitch*100)/100 28 pitchLabel.text = String(pitch) 29 var roll = motion.attitude.roll 30 roll = round(roll*100)/100 31 rollLabel.text = String(roll) 32 } 33 } 34 35 36 override func didReceiveMemoryWarning() { 37 super.didReceiveMemoryWarning() 38 // Dispose of any resources that can be recreated. 39 } 40} 41
あなたの回答
tips
プレビュー