在您希望标准方向的一个视图中,添加一个自定义值,例如 doLandscape: 1。我喜欢在 app.config.Runtime 类中设置运行时“全局”值,否则我会将它附加到导航栏,如果它适用于整个应用程序。
Ext.define(‘MyApp.config.Runtime’,{
singleton : true,
config : {
doLandscape: 0 // initialize to 0
},
constructor : function(config){
this.initConfig(config);
}
});
在视口上,添加:
onOrientationChange: function(viewport, orientation, width, height) {
// test trigger and values
console.log('o:' + orientation + ' w:' + width + ' h:' + height);
if (orientation == 'landscape' && MyApp.config.Runtime.doLandscape === 0) {
// most often this is all that's needed to prevent change
return false;
// alternatively / additionally set it back to portrait manually.
viewport.orientation = this.PORTRAIT;
}
}
查看更新后的源代码,这是处理和触发方向更改的视口默认代码。
onOrientationChange: function() {
var currentOrientation = this.getOrientation(),
newOrientation = this.determineOrientation();
if (newOrientation !== currentOrientation) {
this.fireOrientationChangeEvent(newOrientation, currentOrientation);
}
},
fireOrientationChangeEvent: function(newOrientation, oldOrientation) {
var clsPrefix = Ext.baseCSSPrefix;
Ext.getBody().replaceCls(clsPrefix + oldOrientation, clsPrefix + newOrientation);
this.orientation = newOrientation;
this.updateSize();
this.fireEvent('orientationchange', this, newOrientation, this.windowWidth, this.windowHeight);
}
当您打包原生时,此选项可作为另一种选择:
Ext.device.Orientation.on({
scope: this,
orientationchange: function(e) {
console.log('Alpha: ', e.alpha);
console.log('Beta: ', e.beta);
console.log('Gamma: ', e.gamma);
}
});