- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要对两个横向位置之间的方向变化应用不同的样式,即在 React Native 中从 landscapeRight 到 landscapeLeft 或者反之亦然?我知道如何在纵向和横向之间进行检测,只需要在 REACT NATIVE 中的两个横向位置之间有一个监听器!谢谢!
最佳答案
您必须使用外部依赖项来管理它。像 react-native-orientation-locker
这样的东西可以给你你想要的。此选项要求您使用完整的 react-native
项目,因为它将要求您链接 native 代码,因此它不会在 Expo
。您可以阅读更多相关信息 here .以下是存储库中的基本使用说明。
您可以通过以下方式安装它:
npm install react-native-orientation-locker --save
react-native link react-native-orientation-locker
链接后,您还需要执行一些额外的步骤。
将以下内容添加到您项目的 AppDelegate.m
中:
+#import "Orientation.h"
@implementation AppDelegate
// ...
+- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
+ return [Orientation getOrientation];
+}
@end
安卓
在android/app/src/main/AndroidManifest.xml中添加以下内容
<activity
....
+ android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
....
</activity>
实现 onConfigurationChanged 方法(在 MainActivity.java
中)
// ...
+import android.content.Intent;
+import android.content.res.Configuration;
public class MainActivity extends ReactActivity {
+ @Override
+ public void onConfigurationChanged(Configuration newConfig) {
+ super.onConfigurationChanged(newConfig);
+ Intent intent = new Intent("onConfigurationChanged");
+ intent.putExtra("newConfig", newConfig);
+ this.sendBroadcast(intent);
+ }
// ......
}
然后你可以在你想要访问方向的地方设置一个监听器,通常你在 componentDidMount
中添加它并在 componentWillUnmount
中删除移动它。
这里是一些示例代码,您可以使用它来进行设置。
import Orientation from 'react-native-orientation-locker';
_onOrientationDidChange = (orientation) => {
if (orientation === 'LANDSCAPE-LEFT') {
//do something with landscape left layout
}
if (orientation === 'LANDSCAPE-RIGHT') {
//do something with landscape right layout
}
};
componentWillMount() {
//The getOrientation method is async. It happens sometimes that
//you need the orientation at the moment the js starts running on device.
//getInitialOrientation returns directly because its a constant set at the
//beginning of the js code.
var initial = Orientation.getInitialOrientation();
if (initial === 'PORTRAIT') {
//do stuff
} else {
//do other stuff
}
},
componentDidMount() {
Orientation.getAutoRotateState((rotationLock) => this.setState({rotationLock}));
//this allows to check if the system autolock is enabled or not.
Orientation.lockToPortrait(); //this will lock the view to Portrait
//Orientation.lockToLandscapeLeft(); //this will lock the view to Landscape
//Orientation.unlockAllOrientations(); //this will unlock the view to all Orientations
//get current UI orientation
/*
Orientation.getOrientation((orientation)=> {
console.log("Current UI Orientation: ", orientation);
});
//get current device orientation
Orientation.getDeviceOrientation((deviceOrientation)=> {
console.log("Current Device Orientation: ", deviceOrientation);
});
*/
Orientation.addOrientationListener(this._onOrientationDidChange);
},
componentWillUnmount () {
Orientation.removeOrientationListener(this._onOrientationDidChange);
}
这意味着在您的 _onOrientationDidChange
函数中,它将接收以下内容之一:
'肖像'
'LANDSCAPE-LEFT'
相机左侧主页按钮右侧'LANDSCAPE-RIGHT'
摄像头右侧主页按钮左侧'纵向颠倒'
'未知'
关于react-native - 如何检测 landscapeRight 到 landscapeLeft 方向的变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55295237/
我正在使用 AVFoundation 通过前置摄像头拍摄照片。当设备面向左横向时,一切正常,但当设备面向右横向时,图片会上下颠倒。 func setOutputOrientation() {
在 Windows Phone 8.0 中,我们有 PageOrientation 和 LandscapeLeft、LandscapeRight 等等。现在,我们只有 ApplicationView.
这个问题与当前存在的问题不同。 我已将设备设置为通用,并且只检查了横向。 当我在 iPhone 中启动我的应用程序时,它只能按预期在横向模式下运行。 但是iPad可以旋转,不知道为什么。甚至尝试过 -
我需要对两个横向位置之间的方向变化应用不同的样式,即在 React Native 中从 landscapeRight 到 landscapeLeft 或者反之亦然?我知道如何在纵向和横向之间进行检测,
在我的应用程序中,我将库中的不同图像添加到一个数组中,然后将它们转换为幻灯片放映。我能够做到这一点,但我面临的唯一问题是不同图像的方向。我想要的是,当用户从库中选择图片时,在插入数组之前,应检查其方向
我尝试将 shouldAutorotate 设置为 false supportedInterfaceOrientations 到 UIInterfaceOrientationLandscapeRigh
我正在尝试使用 UILaunchImages 为 LandScapeLeft 和 LandScapeRight 方向提供不同的图像,但只有其中一个被拾取,任何人都可以为 LandScapeLeft 和
我是一名优秀的程序员,十分优秀!