React Native图片滑动组件如何定位点击图片并解决滑动抖动问题
问题1修复:打开时直接定位到点击的图片
你的问题根源是translateX初始值固定为0,仅在触发滑动时才会被赋值为对应初始偏移,修改方式如下:
- 初始化
translateX时直接传入初始偏移:const translateX = useSharedValue(initIndex * -width); - 简化
offsetX初始化逻辑,不需要三元判断:const offsetX = useSharedValue(initIndex * -width); - 删除
onStart回调里的translateX.value = offsetX.value;语句,避免重复赋值
问题2修复:消除触摸时的图片抖动
抖动是因为onStart回调触发时突然修改translateX的值,同时手势触发阈值过小导致的,修改方式如下:
- 已经删除了
onStart里的赋值逻辑,避免无动画的突然偏移 - 将
PanGestureHandler的minDist属性调整为30,过滤细微的触摸误触发 - 调整snap计算逻辑,增加小距离滑动回弹逻辑,避免边界异常偏移
完整修改后代码
import React from "react"; import { Dimensions, StyleSheet, View } from "react-native"; import FastImage from 'react-native-fast-image'; import Animated, { useAnimatedStyle, useSharedValue, useAnimatedRef, useAnimatedGestureHandler, withTiming, useDerivedValue, } from "react-native-reanimated"; import { snapPoint, } from "react-native-redash"; import { PanGestureHandler, TouchableOpacity, } from "react-native-gesture-handler"; const { width, height } = Dimensions.get("window"); export default Swipe = ({route, navigation}) => { const {images, initIndex} = route.params; const index = useSharedValue(initIndex); const snapPoints = images.map((_, i) => i * -width); const panRef = useAnimatedRef(); // 初始化直接传入正确偏移 const offsetX = useSharedValue(initIndex * -width); const translationX = useSharedValue(0); // 初始化直接传入正确偏移,页面加载就显示对应图片 const translateX = useSharedValue(initIndex * -width); const lowVal = useDerivedValue(() => (index.value + 1) * -width); const highVal = useDerivedValue(() => (index.value - 1) * -width); const snapPt = (e) => { "worklet"; return snapPoint(translateX.value, e.velocityX, snapPoints); }; const panHandler = useAnimatedGestureHandler( { // 删掉onStart里的多余赋值,避免抖动 onActive: (event) => { translateX.value = offsetX.value + event.translationX; translationX.value = event.translationX; }, onEnd: (event) => { let val = snapPt(event); let sp = Math.min(Math.max(lowVal.value, val), highVal.value); if (Math.abs(event.translationX) > 10) { translateX.value = withTiming(sp, { duration: 200 }); offsetX.value = sp; } else { // 滑动距离过小时回弹到原位置 translateX.value = withTiming(offsetX.value, { duration: 100 }); } index.value = Math.round(Math.abs(translateX.value/width)); }, }, [] ) const swipeStyle = useAnimatedStyle(() => { return { transform: [{ translateX: translateX.value}] } }); return ( <PanGestureHandler onGestureEvent={panHandler} ref={panRef} minDist={30} // 调大触发阈值,过滤误触 avgTouches > <Animated.View style={styles.container}> <Animated.View style={[{width: width * images.length, height, flexDirection: "row"}, swipeStyle]} > {images.map((img_source, i) => { return ( <View key={i} style={styles.picture}> <View style={styles.image}> <TouchableOpacity onPress={() => navigation.goBack()} style={styles.button}> <FastImage source={{uri:img_source.path}} resizeMode={FastImage.resizeMode.contain} style={styles.image} /> </TouchableOpacity> </View> </View> ); })} </Animated.View> </Animated.View> </PanGestureHandler> ); } const styles = StyleSheet.create({ container: { ...StyleSheet.absoluteFillObject, backgroundColor: "black", }, picture: { width, height, overflow: "hidden", }, image: { flex:1, width: undefined, height: undefined, }, button: { width: width, height:height, } });
内容的提问来源于stack exchange,提问作者user938363
相关产品推荐
相关产品推荐

