Phonegap 2.2.0 - Defining Orientation

I have an application that I would like to save in portrait mode, with the exception of one view in which I want to detect a change in orientation, fire the js event and change the orientation (for this view only).

My application has the following code:

window.shouldRotateToOrientation = function(rotation) { switch (rotation) { case 0: case 180: console.log("Portrait"); return false; //LandscapeRight or LandscapeLeft case 90: case -90: console.log("Landscape"); return false; } } 

This seems to work well when building for iOS5, keeping the application in the Portrait orientation, returning false and shooting correctly when the device is in landscape mode. However, when building for iOS6, the function is called 4 times every time the device rotates, matching each case - making detection useless.

I am approaching this correctly - is there another way or is there something that I am missing?

Please note that I have a very limited understanding of the Xcode / ios / ObjectiveC environment

+4
source share
2 answers

try this code:

 function changeOrientation() { switch (window.orientation) { case 0: // portrait, home bottom case 180: // portrait, home top alert("portrait H: " + $(window).height() + " W: " + $(window).width()); break; case -90: // landscape, home left case 90: // landscape, home right alert("landscape H: " + $(window).height() + " W: " + $(window).width()); break; } } window.onorientationchange = function () { //Need at least 800 milliseconds setTimeout(changeOrientation, 1000); } 

A timeout should fix the problem.

+2
source

The following works for me to determine the orientation:

 function isOrientationPortrait(){ if ($(window).height() > $(window).width()){ return true; } else { return false; } } $(window).on('orientationchange', function () { if(isOrientationPortrait()){ console.log("Portrait"); } else { console.log("Landscape"); } }); 
0
source

All Articles