gpt4 book ai didi

react-native - 在 React Native 中通过平移手势改变元素的样式

转载 作者:行者123 更新时间:2023-12-04 01:26:06 25 4
gpt4 key购买 nike

我有一个按方块划分的屏幕,需要通过用手指连续触摸平移手势来改变它们的颜色。

enter image description here

起初,我认为我可以通过平移处理程序获取我手指的位置并找到哪个元素位于该位置来做到这一点,这样我就可以改变它的颜色。但它并没有像我想象的那样奏效。
我被这个问题困住了,想知道你的意见。

最佳答案

提供PanResponder对于这些方块的父 View 。

// define the constants
const rows = 10;
const columns = 5;
const {width, height} = Dimensions.get('window');

onPanResponderMove 写这个逻辑。
 onPanResponderMove: (evt, gestureState) => {
let i = 0;
let x = evt.nativeEvent.pageX;
let y = evt.nativeEvent.pageY;
let percentageX = Math.floor(x / (width / columns));
let percentageY = Math.floor(y / (height / rows));
i = percentageX + columns * percentageY;
this.onChangeItem(i); // <-- i will provide you the index of current touch
},

在 onChangeItem 函数内部检查移动手指时索引何时发生变化。
onChangeItem = (index) => {
if (this.index !== index) {
// do operations here with index.
}
};

....

这是我的 示例代码 : PanhandlerAnimations

结果:

enter image description here

关于react-native - 在 React Native 中通过平移手势改变元素的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61910153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com