【发布时间】:2020-05-20 08:11:52
【问题描述】:
我正在创建一个可触摸的按钮来响应原生动画。按下按钮时,它应该按比例缩小一点。当压力释放时,它应该会恢复正常。
这是我的代码:
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(以及其中的动画)从未被调用过。
我有另一个具有相同属性的按钮,但我设置了背景颜色而不是缩放,这就像它应该的那样工作。
【问题讨论】:
-
哦,是的,我明白了。也许它与我的模拟器有关。我正在IOS模拟器上进行测试。我将在真实设备上进行测试以检查。感谢您的意见!
标签: react-native react-animated