美文网首页
根据scrollview的滑动,添加悬浮组件

根据scrollview的滑动,添加悬浮组件

作者: sunny635533 | 来源:发表于2022-03-17 17:58 被阅读0次

效果是类似SectionList的分组悬浮的效果,具体看代码:

'use strict';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
    Animated,
    StyleSheet,
} from 'react-native';

export default class StickyHeader extends Component {
    static propTypes = {
        stickyHeaderY: PropTypes.number,//悬浮栏初始高度
        stickyScrollY: PropTypes.any,//scrollview的高度
    }
    static defaultProps = {
        stickyHeaderY: -1,
        stickyScrollY: new Animated.Value(0)
    }

    constructor(props) {
        super(props);
        this.state = {
            stickyLayoutY: 0,
        };
    }

    componentDidMount() {
        this.props.stickyScrollY.addListener((info) => {
            // console.log(info)
        })
    }

    _onLayout = (event) => {
        this.setState({
            stickyLayoutY: event.nativeEvent.layout.y,
        });
    }

    render() {
        const { stickyHeaderY, stickyScrollY, children, style } = this.props
        const { stickyLayoutY } = this.state
        // let y = stickyHeaderY != -1 ? stickyHeaderY : stickyLayoutY;
        const translateY = stickyScrollY.interpolate({
            // inputRange: [-1, 0, y, y + 1],
            // outputRange: [0, 0, 0, 1],
            inputRange: [0, stickyHeaderY, stickyHeaderY + 1],
            outputRange: [0, 0, 1],
        });
         return (
            <Animated.View
                onLayout={this._onLayout}
                style={[
                    style,
                    styles.container,
                    { transform: [{ translateY }] }//translateY
                ]
                }
            >
                {children}
            </Animated.View>
        );
    }
}

// define your styles
const styles = StyleSheet.create({
    container: {
        zIndex: 100
    },
});




具体使用组件:

<Animated.ScrollView style={styles.container}
                                         ref={ref => this.scroll2 = ref}
                                         refreshControl={refreshControl}
                                         onScrollEndDrag={this._onEndReached}
                                         onEndReachedThreshold={0.1}
                                         scrollEventThrottle={1}
                                         onScroll={Animated.event([{nativeEvent: {contentOffset: {y: this.state.scrollY,}}}], {useNativeDriver: true})}>
                     
                    </Animated.ScrollView>
image.png

相关文章

网友评论

      本文标题:根据scrollview的滑动,添加悬浮组件

      本文链接:https://www.haomeiwen.com/subject/ictbdrtx.html