RN框架多设备开发指导-触控板PC场景
1.1 场景概述
在多设备系统开发中,不同设备的屏幕形态各异,如直板机、PAD、PC、折叠屏等,同时系统状态栏、导航栏、软键盘等元素也会占据屏幕空间。为了确保触控板-PC界面在各种设备和场景下都能正常显示,提供流畅的用户体验。本文将详细介绍 RN 触控板-PC组件的实现原理、适配指导以及具体的场景案例。
1.1.1 使用场景
在多设备系统、文件管理、内容浏览、应用界面等应用中,触控板-PC功能是很常见的功能,希望在不同设备上触控板-PC界面都能正常显示和交互。下面是不同设备上触控板-PC界面的差异,包括:直板机、PAD、PC、折叠屏(阔折叠、双折叠、三折叠)、智慧屏、座舱。
1.1.2 常见问题
开发过程中触控板-PC界面在不同设备上会存在差异,可能会出现以下问题:
• 触控板-PC布局在不同设备上显示不一致
• 卡片网格列数不正确导致布局错乱
• 横竖屏切换时布局调整不正确
• 分屏场景下交互元素被另一个应用窗口遮挡
• 滚动体验在不同设备上不一致
1.1.3 多设备适配
1.1.3.1 触控板-PC多设备尺寸自适应适配
说明
根据断点布局实现触控板-PC界面的自适应:
| 横向断点 | sm | md | lg | xl |
| 属性 | 适配竖屏手机,界面自适应 | 适配横屏平板,界面自适应 | 适配折叠屏,界面自适应 | 适配PC端,界面自适应 |
|
|
|
|
|
|
1.1.3.2 触控板-PC多设备窗口变化适配
• 适配点1:基于断点实现组件自适应在多设备布局适配,窗口响应(横竖屏、分屏、折叠开合等)



• 适配点2:组件尺寸自适应覆盖拉伸、均分、占比、缩放、延伸、隐藏、折行



1.2 开发指导
1.2.1 RN开发
1.2.1.1 关键能力
通过 React Native 组件实现触控板-PC功能,使用 react-native-gesture-handler 库实现手势识别,使用 react-native-reanimated 库实现动画效果,使用 @hadss/react_native_adaptive_layout 库实现断点布局。
核心依赖:
{
"react-native-gesture-handler": "^2.10.0",
"react-native-reanimated": "^3.0.0",
"@hadss/react_native_adaptive_layout": "^1.0.0-rc.2"
}自适应实现:
• 断点布局: 根据不同设备尺寸设置网格列数
const gridColumns = useBreakpointValue({
base: 1,
sm: 1,
md: 2,
lg: 3,
xl: 4,
}) || 2;• 自适应卡片尺寸: 根据屏幕尺寸动态调整卡片宽度
width: `${100 / gridColumns - 2}%`,
marginHorizontal: '1%',• 自适应分栏比例: 根据屏幕尺寸动态调整分栏比例
const [splitRatio, setSplitRatio] = useState(0.5);
const leftW = winW * splitRatio;
const rightW = winW * (1 - splitRatio);1.2.1.2 指导案例
关键实现:
1. 手势识别: 使用 react-native-gesture-handler 实现拖拽、滑动、双指缩放等手势
• 拖拽手势: 使用 LongPress 和 Pan 手势实现卡片拖拽
const createDragGesture = (itemId: string) => {
const longPress = Gesture.LongPress()
.minDuration(500)
.onStart(() => {
isDraggingSV.value = true;
dragItemIdSV.value = itemId;
const found = FACE_ITEMS.find(f => f.id === itemId);
if (found) {
dragOriginalImageSV.value = found.image;
isNearBottomSV.value = false;
runOnJS(setDragImageSource)(found.image);
}
});
const pan = Gesture.Pan()
.onUpdate((event) => {
if (isDraggingSV.value) {
dragX.value = event.absoluteX;
dragY.value = event.absoluteY;
const nearBottom = event.absoluteY > winH - 180;
shortcutAnim.value = withTiming(nearBottom ? 1 : 0, { duration: 120 });
if (nearBottom !== isNearBottomSV.value) {
isNearBottomSV.value = nearBottom;
runOnJS(setDragImageSource)(nearBottom ? IMAGES.highlight : dragOriginalImageSV.value);
}
}
})
.onEnd(() => {
isDraggingSV.value = false;
dragItemIdSV.value = null;
shortcutAnim.value = withTiming(0, { duration: 200 });
runOnJS(setDragImageSource)(null);
});
return Gesture.Simultaneous(longPress, pan);
};• 双指手势: 使用 Pinch 和 Rotation 手势实现缩放和旋转
const pinchGesture = Gesture.Pinch()
.onUpdate((e) => {
scaleShared.value = e.scale;
});
const rotationGesture = Gesture.Rotation()
.onUpdate((e) => {
rotationShared.value = (e.rotation * 180) / Math.PI;
});
const multiTouchGestures = Gesture.Simultaneous(pinchGesture, rotationGesture);• 滑动切换: 使用 Pan 手势实现滑动切换
const swipeGesture = Gesture.Pan()
.maxPointers(1)
.minDistance(10)
.onEnd((event) => {
runOnJS(handleSwipeEnd)(event);
});1.2.1.3 示例代码
场景Sample示例代码地址:RN 三方框架赋能指导配套代码
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要发帖子









































苏公安网备 32011402010933号