[英]Phonegap 2.2.0 - Detecting orientation
I have an app that I'd like to keep in portrait mode, except for a single view in which I want to detect an orientation change, fire a js event and change the orientation (for that view only). 我有一个应用程序想要保持纵向模式,除了要检测方向更改,触发js事件并更改方向的单个视图(仅适用于该视图)。
I have the following code in my app: 我的应用程序中包含以下代码:
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 okay when built for iOS5, keeping the app in Portrait orientation by returning false, and correctly firing when the device is in landscape mode. 当为iOS5构建时,这似乎可以正常工作,通过返回false保持应用程序为纵向,并在设备处于横向模式时正确触发。 However when built for iOS6 the function is called 4 times each time the device the device is rotated, matching each case - rendering the detection useless. 但是,当为iOS6构建时,每次旋转设备都会调用该函数4次,每种情况都匹配-使检测无用。
Am I approaching this correctly - is there another way or is there something that I am missing? 我是否正确地解决了这一问题-是否有其他方法或缺少某些东西?
Note that I have a very limited understanding of the XCode/ios/ObjectiveC environment 请注意,我对XCode / ios / ObjectiveC环境的了解非常有限
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);
}
The timeout should fix the problem. 超时应该解决问题。
The following works for me to detect 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");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.