Ste*_*sse 5 javascript reactjs react-native react-animated
这是我迄今为止所拥有的视频https://drive.google.com/file/d/1tJMZfpe8hcqLcNMYiE-6pzwHX0eLofv1/view?usp=sharing
我试图在 contentOffset.y 到达标题后修复“可用 3 个月”的问题。与 css 中的 a 类似position: sticky。
到目前为止,我想通过onScroll滚动视图组件中的 prop 来完成此操作,但问题是,我已经有来自父组件和子组件的动画(Animated.Event),这意味着我执行此操作的唯一方法是通过但是listener如果该选项设置为 false Animated.Event,那么这样做会导致超级断断续续的动画useNativeDriver。
如果我将其设置为 true,则整个事情将无法正常工作(它会崩溃)。错误是这样的:“onScroll 不是一个函数,它是 Animated.event 的一个实例”
所以,假设我们有两个组件,父组件是Parent.js,子组件(滚动视图)是ChildScrollView.js
滚动视图上已经ChildScrollView.js有动画,但我们需要在组件中添加更多动画Parent.js,以处理ChildScrollView.js无法处理的导航
所以它的编码是这样的:
Parent.js
componentWillMount() {
const { navigation } = this.props
const { scrollY } = this.state
const bgColor = scrollY.interpolate({
inputRange: [HEADER_HEIGHT / 4, HEADER_HEIGHT / 2],
outputRange: ['transparent', 'rgb(255,255,255)'],
extrapolate: 'clamp',
})
const titleColor = scrollY.interpolate({
inputRange: [0, HEADER_HEIGHT / 2],
outputRange: ['transparent', colors.paragraphs],
extrapolate: 'clamp',
})
const titleMarginTop = scrollY.interpolate({
inputRange: [0, HEADER_HEIGHT / 2],
outputRange: [HEADER_HEIGHT, 0],
extrapolate: 'clamp',
})
navigation.setParams({
bgColor,
titleColor,
titleMarginTop,
})
}
onScroll() {
}
render() {
return (
<ChildScrollView
{...childProps}
onScroll={Animated.event([
{ nativeEvent: { contentOffset: { y: scrollY } } },
], {
useNativeDriver: false, // when I do this, it become super laggy, but setting it to true makes the app crash
listener: this.onScroll,
})}
>
<MakeMeFixedOnScroll>I could do this in css with "position: sticky"</MakeMeFixedOnScroll>
</ChildScrollView>
)
}
Run Code Online (Sandbox Code Playgroud)
而孩子也同样如此,
<Animated.ScrollView
{...props}
onScroll={Animated.event(
[{ nativeEvent: { contentOffset: { y: scrollY } } }],
{
useNativeDriver: false,
listener: event => {
if (onScroll) onScroll(event)
},
}
)}
scrollEventThrottle={16}
>
Run Code Online (Sandbox Code Playgroud)
我会使用SectionList
<SectionList
renderItem={({item, index, section}) => (
<Text style={styles[item.type]}>{item.text}</Text>
)}
renderSectionHeader={({ section: { title } }) => (
title && <Text style={styles.sticky}>{title}</Text>
)}
sections={[
{ title: null, data: [{
type: 'heading', text: '133 Random Road'
}, {
type: 'heading', text: 'Donnybrook'
}, {
type: 'subtitle', text: 'Dublin 4'
}, {
type: 'title', text: 'From E1000/month'
}]
},
{ title: 'Available For 3 Month', data: [{
type: 'text', text: 'Beautiful for bedroom...'
}]
}
]}
stickySectionHeadersEnabled
/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4845 次 |
| 最近记录: |