用动画缩放反应原生按钮

yes*_*day 3 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>
)
}
Run Code Online (Sandbox Code Playgroud)

当按钮被按下时,pressInHandler 中的动画被启动,并且缩放从 1 到 0.95 的动画。这有效。但是当我释放压力时(onPressOut 被调用),比例会在没有平滑动画的情况下恢复为 1。似乎从来没有调用过 pressOutHandler(以及其中的动画)。

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

Asw*_*n C 6

让它变得简单。

注意:始终使用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,
  },
});
Run Code Online (Sandbox Code Playgroud)