2017-04-08 1 views
0

가속도계, 나침반 또는 기타에 액세스하여 Cordova에서 물리적 장치 방향을 감지하는 방법이 있습니까?코도바에서 장치 방향 감지

기기가 가로로 (즉, 가로로) 또는 세로로 (즉 세로로) 있는지 감지하고 싶습니다. 이외 : cordova detecting orientation changewindow.orientation을 통해 오리엔테이션을 사용할 수 없게됩니다. 사용자가 오리엔테이션을 잠근 경우 실패하기 때문입니다.

사용자가있는 모드를 알 필요가 없습니다. 기기가 옆으로 세워져 있는지를 알아야합니다.

+0

[코드 변화 감지 방향 변경] (http://stackoverflow.com/questions/28283061/cordova-detecting-orientation-change) – str

+0

포인터 주셔서 감사합니다. 나는 그 질문을 편집했다. 제안 된 해결책은 잠금 장치 방향을 다루지 않습니다. – zaph0t

답변

0

how device orientation can be read from accelerometer despite a locked device orientation을 설명하는 블로그 항목을 찾았습니다.

관련 코드 스 니펫은 Objective-C에서 제공되지만 Cordova's Device Motion Plugin을 통해 번역되고 사용될 수 있으므로 Phonegap에 적용됩니다.

float x = -[acceleration x]; 
float y = [acceleration y]; 
float angle = atan2(y, x); 

if(angle >= −2.25 && angle <= −0.75) { 
    //OrientationPortrait 
} else if(angle >= −0.75 && angle <= 0.75){ 
    //OrientationLandscapeRight 
} else if(angle >= 0.75 && angle <= 2.25) { 
    //OrientationPortraitUpsideDown 
} else if(angle <= −2.25 || angle >= 2.25) { 
    //OrientationLandscapeLeft]; 
} 

설명 : 여기서

원래의 Zero, ATAN2에게 모두 동일하지 인수 x 및 y는 임의의 실제 (y, x)가 양의 X 축과 라디안의 각도 인 축 및 지정된 좌표로 지정된 점을 지정합니다. 각도는 반 시계 방향 각도에서는 양수이고 시계 방향 각도에서는 음수입니다.