我如何在我的滚动视图上放置点

时间:2019-03-18 07:08:54

标签: react-native

我正在使用以下代码在其底部显示滚动视图和点。现在,我想在滚动视图上方显示这些点,但是我不知道该怎么做。

我的代码

<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
    <View
      style={{ width, height: width }}
      >
      <ScrollView
        horizontal={true}
        pagingEnabled={true}
        showsHorizontalScrollIndicator={false}
        onScroll={Animated.event(
          [{ nativeEvent: { contentOffset: { x: this.scrollX } } }]
        )} 
        scrollEventThrottle={16}
        >
        {this.state.dataSource.map((source, i) => { 
          return ( 
            <Image
              key={i}
              style={{ width, height: width }}
              source={{uri: source.image}}
            />
          );
        })}
      </ScrollView>
    </View>
    <View
      style={{ flexDirection: 'row' }}
      >
      {this.state.dataSource.map((_, i) => { 
        let opacity = position.interpolate({
          inputRange: [i - 1, i, i + 1],
          outputRange: [0.3, 1, 0.3],
          extrapolate: 'clamp' 
        });
        return (
          <Animated.View 
            key={i}
            style={{ opacity, height: 5, width: 5, backgroundColor: '#595959', margin: 2, borderRadius: 5 }}
          />
        );
      })}
    </View>
  </View>

1 个答案:

答案 0 :(得分:0)

我吓到你还在学习本机/反应。您已经找到解决方案的一半,您只需要将点视图放在滚动视图上方即可。

render(){
return(
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
    <View
      style={{ width, height: width }}
      >
{this.renderDotsView(this.state.dataSource, position)}
      <ScrollView
        horizontal={true}
        pagingEnabled={true}
        showsHorizontalScrollIndicator={false}
        onScroll={Animated.event(
          [{ nativeEvent: { contentOffset: { x: this.scrollX } } }]
        )} 
        scrollEventThrottle={16}
        >
        {this.state.dataSource.map((source, i) => { 
          return ( 
            <Image
              key={i}
              style={{ width, height: width }}
              source={{uri: source.image}}
            />
          );
        })}
      </ScrollView>
    </View>

  </View>
)}

renderDotsView = (array, position) =>{
return(
 <View
      style={{ flexDirection: 'row' }}
      >
      {array.map((_, i) => { 
        let opacity = position.interpolate({
          inputRange: [i - 1, i, i + 1],
          outputRange: [0.3, 1, 0.3],
          extrapolate: 'clamp' 
        });
        return (
          <Animated.View 
            key={i}
            style={{ opacity, height: 5, width: 5, backgroundColor: '#595959', margin: 2, borderRadius: 5 }}
          />
        );
      })}
    </View>
)
}