I have some <AnimatedButton />
I want to animate it to go from 100% width to 40% width depending on a prop that is a boolean called isFullWidth
.
I have:
class AnimatedButton extends Component {
constructor(props) {
super(props);
this.state = { width: new Animated.Value(100) };
}
toggleWidth() {
const endWidth = this.props.isFullWidth ? 40 : 100;
Animated.timing(this.state.width, {
toValue: endWidth,
duration: 200,
easing: Easing.linear,
}).start();
}
render() {
<TouchableOpacity
style={{ width: `${this.state.width}%` }}
onPress={this.props.onPress}
>
// more stuff
</TouchableOpacity>
}
}
The problem is that it just jumps into the appropriate percentage without animating. I tried setting the width to just this.state.animatedValue
and rather than use percentage, just use pixels, e.g. 150 to 400 and back, and it works fine as expected.
Same question applies for going from say rgba(220, 100, 50, 0.8)
to rgba(30, 70, 30, 1.0)
and back?