代码之家  ›  专栏  ›  技术社区  ›  Saeed Heidarizarei

向下滚动时反应本机淡入右/左视图

  •  4
  • Saeed Heidarizarei  · 技术社区  · 6 年前

    我有3个 View 在一个 ScrollView 我怎么能左右都消失 查看

    <ScrollView >
      <View>
        <Text>Fade Right View 1</Text>
      </View>
    
      <View>
        <Text>Fade Right View 2</Text>
      </View>
    
      <View>
        <Text>Fade Right View 3</Text>
      </View>
    </ScrollView >
    

    像这样的: 滚动时元素淡入( https://codepen.io/annalarson/pen/GesqK

    2 回复  |  直到 6 年前
        1
  •  5
  •   Tim    6 年前

    我已经为您创建了一个小示例,但您当然需要对其进行微调,以使其完全符合您的目的。

    演示

    gif

    解释

    我的例子由两部分组成。淡入度组件和实际的滚动视图。淡入度组件处理动画。通过滚动ScrollView(请参见handleScroll函数)触发淡入动画。

    淡入分量

    class Fade extends Component {
      constructor(props) {
        super(props);
        this.state = {
          visible: props.visible,
          visibility: new Animated.Value(props.visible ? 1 : 0),
        };
      };
    
      componentWillReceiveProps(nextProps) {
        if (nextProps.visible) {
          this.setState({ visible: true });
        }
        Animated.timing(this.state.visibility, {
          toValue: nextProps.visible ? 1 : 0,
          duration: 500,
        }).start(() => {
          this.setState({ visible: nextProps.visible });
        });
      }
    
      render() {
        const { style} = this.props;
    
        const containerStyle = {
          opacity: this.state.visibility.interpolate({
            inputRange: [0, 1],
            outputRange: [0, 1],
          }), // interpolate opacity 
          transform: [
            {
                translateX: this.state.visibility.interpolate({
                    inputRange: [0, 1],
                    outputRange: [-20, 0],
                }), // interpolate translateX to create a fade in left/right
            },
          ],
        };
    
        const combinedStyle = [containerStyle, style];
        return (
          <Animated.View style={this.state.visible ? combinedStyle : containerStyle} />
        );
      }
    }
    

    滚动视图片段

    handleScroll(e) {
        if (e.nativeEvent.contentOffset.y > 50) { // you need to fine tune this value
          this.setState({ visible: true }); 
        }
      }
    
    
    <ScrollView onScroll={(e) => this.handleScroll(e) } scrollEventThrottle={8}>
              <View style={{ backgroundColor: 'yellow', height: 200, marginTop: 10 }}/>
              <View style={{ backgroundColor: 'yellow', height: 200, marginTop: 10 }}/>
              <View style={{ backgroundColor: 'yellow', height: 200, marginTop: 10 }}/>
              <Fade style={{ backgroundColor: 'red', height: 200, marginTop: 10 }} visible={this.state.visible} />
    </ScrollView>
    

        2
  •  -1
  •   Mohsen    6 年前

    你可以用 onScroll 这样地:

    <ScrollView onScroll={this.handleScroll} />
    

    之后:

    handleScroll = (event: Object) => {
     console.log(event.nativeEvent);
     // You see {layoutMeasurement, contentOffset, contentSize} in nativeEvent
    }
    

    contentOffset layoutMeasurement contentSize 你可以重写 Element Fade In on Scroll