react-native - 用动画缩放 react 原生按钮

标签 react-native react-animated

我正在创建一个可触摸按钮以响应原生动画。按下按钮时,它应该缩小一点。当压力释放后,它应该会恢复正常。

这是我的代码:

export const TouchableButton = (props) => {

    const { onPress, text, icon } = props

    const animatedValue = new Animated.Value(0)

    const animatedValueInterpolateScale = animatedValue.interpolate({
        inputRange: [0, 1],
        outputRange: [1, 0.95]
    })

    const pressInHandler = () => {
        Animated.timing(
            animatedValue,
            {
                toValue: 1,
                duration: 150
            }
        ).start()
    }

    const pressOutHandler = () => {
        Animated.timing(
            animatedValue,
            {
                toValue: 0,
                duration: 150
            }
        ).start()
    }

return (
    <TouchableWithoutFeedback onPress={onPress} onPressIn={pressInHandler} onPressOut={pressOutHandler}>
        <View style={{ alignItems: 'center' }}>
            <Animated.View style={{ width: '100%', height: 40, borderRadius: 5, overflow: 'hidden', transform: [{ scaleX: animatedValueInterpolateScale }, { scaleY: animatedValueInterpolateScale }] }}>
                <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor: Color.GrayLight }}>
                    <Text style={{ marginTop: 2.5, fontFamily: 'AlegreyaSans-Medium', fontSize: 15, color: Color.White }}>{text}</Text>
                    <View style={{ position: 'absolute', left: 12.5, top: 12.5 }}>
                        <Icon lib={icon.lib} icon={icon.icon} color={Color.White} size={15} />
                    </View>
                </View>
            </Animated.View>
        </View>
    </TouchableWithoutFeedback>
)
}

当按下按钮时,pressInHandler 中的动画开始,比例从 1 到 0.95 动画。这行得通。但是,当我释放压力(调用 onPressOut)时,比例会迅速恢复到 1,而不会出现流畅的动画。似乎从未调用过 pressOutHandler(及其中的动画)。

我有另一个具有相同属性的按钮,但我没有缩放,而是设置了背景颜色,这就像它应该的那样工作。

最佳答案

让它变得简单。

注意:始终使用 useNativeDriver: true

const App = () => {
  const animation = new Animated.Value(0);
  const inputRange = [0, 1];
  const outputRange = [1, 0.8];
  const scale = animation.interpolate({inputRange, outputRange});

  const onPressIn = () => {
    Animated.spring(animation, {
      toValue: 1,
      useNativeDriver: true,
    }).start();
  };
  const onPressOut = () => {
    Animated.spring(animation, {
      toValue: 0,
      useNativeDriver: true,
    }).start();
  };

  return (
    <View style={styles.container}>
      <Animated.View style={[styles.button, {transform: [{scale}]}]}>
        <TouchableOpacity
          style={styles.btn}
          activeOpacity={1}
          onPressIn={onPressIn}
          onPressOut={onPressOut}>
          <Text style={styles.btnText}>BUTTON</Text>
        </TouchableOpacity>
      </Animated.View>
    </View>
  );
};

export default App;

const styles = StyleSheet.create({
  container: {flex: 1, alignItems: 'center', justifyContent: 'center'},
  button: {
    height: 70,
    width: 200,
    backgroundColor: 'red',
    marginBottom: 20,
    borderRadius: 10,
  },
  btn: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  btnText: {
    color: '#fff',
    fontSize: 25,
  },
});

关于react-native - 用动画缩放 react 原生按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61908202/

相关文章:

android - React Native Android 状态栏图标颜色

android - 如何在 React Native 中实现类似 CoordinatorLayout 的功能?

react-native - 带有标记 1 的动画节点不存在

javascript - react native 圆变换翻译动画

javascript - 运行变量是 NaN,但调试变量不是 NaN。为什么?

ios - pod install 正在安装在不同的文件夹中

javascript - 如何在 React Native 中刷新屏幕?

reactjs - 对 native 搜索使用react并滚动以点击

animation - 在 React Native Animated 中使用 useNativeDriver 时出错