我想在我的应用程序中添加一个可折叠的标题。我为 Header 视图创建了一个单独的组件。
interface Props{
userData: UserDetails | null
scrollY: Animated.Value
}
const HEADER_EXPANDED_HEIGHT = sizeProportionHeight(320)
const HEADER_COLLAPSED_HEIGHT = sizeProportionHeight(142)
const CollapsableHeader : React.FC<Props> = ({userData, scrollY}) => {
const headerHeight = scrollY.interpolate({
inputRange: [0, HEADER_EXPANDED_HEIGHT-HEADER_COLLAPSED_HEIGHT],
outputRange: [HEADER_EXPANDED_HEIGHT, HEADER_COLLAPSED_HEIGHT],
extrapolate: 'clamp'
})
return(
<Animated.View style={{height: headerHeight, width: SCREEN_WIDTH, position:'absolute', top:0, left: 0}}/>
)
}
export default CollapsableHeader
在我的主页上,我添加了这样的标题:
interface Props{
navigation: StackNavigationProp<MainParamList,'HomeScreen'>
route: RouteProp<MainParamList,'HomeScreen'>
}
interface HeaderState {
scrollY: Animated.Value
}
interface HomeScreenState {
header: HeaderState
}
const HomeScreen : React.FC<Props> = ({ navigation, route }) => {
const [state, setState] = React.useState<HomeScreenState>({header: {scrollY: new Animated.Value(0)}})
return (
<View style={styles.container}>
<CollapsableHeader userData={null}
scrollY={state.header.scrollY}/>
<ScrollView contentContainerStyle={[styles.scrollContainer]}
onScroll={Animated.event(
[{
nativeEvent: {
contentOffset: {
y: state.header.scrollY
}
},
}],
{
useNativeDriver:true
}
)}
scrollEventThrottle={16}>
/// Scrollable content
</ScrollView>
</View>
)
}
但是,如果我尝试滚动,我会收到一条错误消息
this.props.onScroll is not a function. (In 'this.props.onScroll(e)', 'this.props.onScroll' is an instance of AnimatedEvent)
如果我删除useNativeDriver: true
错误消失,而是我收到警告说我失踪了useNativeDriver
。
如何正确使用此可折叠动画?