[英]Animating toolbar elevation on scroll in React-Native
I am trying to animate toolbar elevation on scroll of a flatlist, But I keep getting a Warning: Failed prop type: Invalid prop style.container of type object supplied to Toolbar, expected number
. 我正在尝试在平面列表的滚动上为工具栏的高度设置动画,但是我不断收到
Warning: Failed prop type: Invalid prop style.container of type object supplied to Toolbar, expected number
。 I am using Toolbar component from react-native-material-ui . 我正在使用react-native-material-ui中的 工具栏组件。 I am using Animated API for the animation.
我正在为动画使用Animated API。
Code Snippet: 代码段:
state = {
scrollY: new Animated.Value(0)
};
render() {
const elevate = this.state.scrollY.interpolate({
inputRange: [0, 1],
outputRange: [0, 7],
extrapolate: 'clamp'
});
return (
<ThemeProvider uiTheme={uiTheme}>
<View style={styles.contentWrapper}>
<CustomStatusBar themeColor={uiTheme.palette.primaryColor} elevation={elevate}/>
<View>
<Toolbar
leftElement="menu"
centerElement="Aloha"
searchable={{
autoFocus: true,
placeholder: 'Search your chats',
}}
onLeftElementPress={() => this.props.navigation.navigate('DrawerOpen')}
style={{container: {elevation: elevate}}}
/>
</View>
And I am using the onScroll
prop of flatlist
as follows: 我正在使用
onScroll
的flatlist
,如下所示:
onScroll={Animated.event(
[{nativeEvent: {contentOffset: {y: this.state.scrollY}}}]
)}
OUTPUT IN CONSOLE 控制台输出
e {_children: Array(0), _parent: e, _config: {…}, _interpolation: ƒ}
_children
:
(2) [e, e]
_config
:
{inputRange: Array(2), outputRange: Array(2), extrapolate: "clamp"}
_interpolation
:
ƒ (t)
_parent
:
e {_children: Array(1), _value: 0, _startingValue: 0, _offset: 0, _animation: null, …}
__proto__
:
t
Try this: 尝试这个:
constructor(props) {
super(props);
this.elevation = new Animated.Value(0);
this.state = {
scrollY: new Animated.Value(0)
};
}
changeElevation = () => {
var elevation = 7;
if (this.state.scrollY === 0) elevation = 0;
Animated
.timing(this.elevation, {
toValue: elevation,
duration: ANIMATION_DURATION //could be 200
})
.start();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.