iPad orientationchange problems..
What this code is supposed to do is change those properties when the iPad switches orientation, and it does in the Simulator and on my iPad. But the problem is that it switches whenever there is any slight movement. Like say I have my iPad in Portrait mode and I'm just walking around it switches back and forth from the Portrait and Landscape settings I specified, even though the device hasn't been turned on it's side to Landscape.
Here is my code..
//////////ORIENTATION
win1.orientationModes = [
Titanium.UI.PORTRAIT,
Titanium.UI.UPSIDE_PORTRAIT,
Titanium.UI.LANDSCAPE_LEFT,
Titanium.UI.LANDSCAPE_RIGHT,
Titanium.UI.FACE_UP,
Titanium.UI.FACE_DOWN
];
function getOrientation(o) {
switch (o) {
case Titanium.UI.PORTRAIT: return 'portrait';
case Titanium.UI.UPSIDE_PORTRAIT: return 'portrait';
case Titanium.UI.LANDSCAPE_LEFT: return 'landscape';
case Titanium.UI.LANDSCAPE_RIGHT: return 'landscape';
}
}
Ti.Gesture.addEventListener('orientationchange',function(e){
var orientation = getOrientation(e.orientation);
if (orientation == 'portrait') {
header.bottom = 800;
header.left = 34;
view.left = 34;
view.bottom = 0;
view.height = 800;
dateDisplay.bottom = 675;
dateDisplay.left = 635;
} else {
header.bottom = 550;
header.left = 300;
view.height = 550;
view.left = 300;
dateDisplay.bottom = 423;
dateDisplay.left = 900;
}
});
Has anyone else had problems with this or did I just do something wrong. Any help would be great! Thanks!
4 Answers
-
@Juan Pons Thanks, I did that but it didn't fix the problem.
FIXED IT!
However I did find a solution. I added an if landscape and it worked.. check the code below.//////////ORIENTATION win1.orientationModes = [ Titanium.UI.PORTRAIT, Titanium.UI.UPSIDE_PORTRAIT, Titanium.UI.LANDSCAPE_LEFT, Titanium.UI.LANDSCAPE_RIGHT ]; function getOrientation(o) { switch (o) { case Titanium.UI.PORTRAIT: return 'portrait'; case Titanium.UI.UPSIDE_PORTRAIT: return 'portrait'; case Titanium.UI.LANDSCAPE_LEFT: return 'landscape'; case Titanium.UI.LANDSCAPE_RIGHT: return 'landscape'; } } Ti.Gesture.addEventListener('orientationchange',function(e){ var orientation = getOrientation(e.orientation); if (orientation == 'portrait') { header.bottom = 800; header.left = 34; view.left = 34; view.bottom = 0; view.height = 800; dateDisplay.bottom = 675; dateDisplay.left = 635; } else if (orientation == 'landscape') { header.bottom = 550; header.left = 300; view.height = 550; view.left = 300; dateDisplay.bottom = 423; dateDisplay.left = 900; } else { //do nothing } });
Hope this helps someone! It works perfectly now!
-
Ross,
I've seen similar issues, and I believe that is has to do with the Face_Up and Face_Down events.
Try removing these two lines from your orientationModes and see if that helps
Titanium.UI.FACE_UP, Titanium.UI.FACE_DOWN
-
I had the same issue. I think the iPad fires orientationchange events for every ever so slight change (that's how you roll an airplane in x-plane). This would explain why your code for "landscape" was executing every time a change event came in, before you added the "if statement"…
-
Shake also triggers orientationchange maybe the root cause.
http://developer.appcelerator.com/question/128920/shake-also-triggers-orientationchange