在日历组件中滚动到当前时间

时间:2018-11-16 15:43:35

标签: react-native scrollview

我正在使用react-native创建自己的日历组件。一天中的小时是垂直显示的,我想滚动到当前时间,水平红色线显示该时间。

我使用Moment通过以下方法查找当前的小时和分钟:

  _findCurrentTime() {
    const offset = 100;
    const { format24h } = this.props;
    const { width, styles } = this.props;
    const timeNowHour = moment().hour();
    const timeNowMin = moment().minutes();
    const val = offset * (timeNowHour - this.props.start) + (offset * timeNowMin) / 60
    return val;
  } 

我想使用scrollView.scrollTo滚动到当前时间

  <ScrollView
    ref={scrollView => {
      if (scrollView !== null) {
        this.scrollView = scrollView;
        scrollView.scrollTo({
          x: 0,
          y: scrollOffset,
          animated: true
        });
      }
    }}
    contentContainerStyle={[
      styles.contentStyle,
      { width: this.props.width }
    ]}
  >
    {this._renderLines()}
    {this._renderEvents()}
    {this._renderRedLine()}
  </ScrollView>

如何滚动到当前时间,所以当前时间始终位于视图的中心?我如何设置scrollOffset

0 个答案:

没有答案