我正在尝试创建一个调整大小的按钮(按下时会变小一点)。我使用 TouchableWithoutFeedbackreact-native-gesture-handler
并使用react-native-reanimated
.
到目前为止,这是我的代码:
import React, { useState } from 'react';
import { View } from 'react-native';
import Animated, { Easing, Extrapolate } from 'react-native-reanimated';
import { TouchableWithoutFeedback } from 'react-native-gesture-handler';
const { interpolate, sub } = Animated;
const TouchableResize = (props) => {
const { onPress, children } = props;
const [scale, setScale] = useState(0);
const scaling = interpolate(scale, {
inputRange: [0, 1],
outputRange: [1, 0.90],
extrapolate: Extrapolate.CLAMP
});
return (
<TouchableWithoutFeedback onPressIn={() => setScale(1)} onPressOut={() => setScale(0)}>
<Animated.View style={{ transform: [{ scaleX: scaling }, { scaleY: scaling }] }}>
{children}
</Animated.View>
</TouchableWithoutFeedback>
);
};
export { TouchableResize };
此代码部分有效。按下按钮时,按钮大小调整为 0.90,但动画不流畅。它直接捕捉到 0.90,当它释放时,按钮直接捕捉回来。
如何更新我的代码以使动画顺利运行?请注意,我是 react-native-reanimated 的完整初学者。