React Native - 如何从ScrollView获取Y偏移值视图? [英] React Native - How to get Y Offset Value of a view from ScrollView?

查看:136
本文介绍了React Native - 如何从ScrollView获取Y偏移值视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取视图的滚动位置。但 Y偏移到页面的值与视图的位置无关。

I am trying to get the scroll position of a view. But the value for Y offset to page which is not related to the view's position.

ScrollView层次结构:

<ScrollView>
  - MyComponent1
  - MyComponent2
    - SubView1
       - SubView2
         - <View> (Added ref to this view and passing Y offset value through props)
  - MyComponent3
 </ScrollView>

SubView2组件:

this.myComponent.measure( (fx, fy, width, height, px, py) => {
   console.log('Component width is: ' + width)
   console.log('Component height is: ' + height)
   console.log('X offset to frame: ' + fx)
   console.log('Y offset to frame: ' + fy)
   console.log('X offset to page: ' + px)
   console.log('Y offset to page: ' + py)

   this.props.moveScrollToParticularView(py)
})

<View ref={view => { this.myComponent = view; }}>

我检查了 SubView2 查看 onScroll 方法。但确实与度量值匹配。我可以搞清楚度量值是错误的。

I have checked the exact position of a SubView2 view on onScroll method. But did match with the measure value. I can figure it out the measure value is wrong.

是否存在ScrollView层次结构问题?

Is it ScrollView hierarchy problem?

推荐答案

View 组件有一个名为 <$的属性C $ C> onLayout 。您可以使用此属性来获取该组件的位置。

View component has a property called onLayout. You can use this property to get the position of that component.


onLayout

在安装和布局更改时调用:

Invoked on mount and layout changes with:

{nativeEvent: { layout: {x, y, width, height}}}

计算布局后立即触发此事件,
但是当收到事件$​​ b $ b时,新布局可能还没有反映在屏幕上,特别是如果布局动画在
进度中。

This event is fired immediately once the layout has been calculated, but the new layout may not yet be reflected on the screen at the time the event is received, especially if a layout animation is in progress.

更新

onLayout prop给父组件一个位置。这意味着要找到 SubView2 的位置,您需要获得所有父组件的总和( MyComponent2 + SubView1 + SubView2 )。

onLayout prop gives a position to the parent component. This means to find the position of SubView2, you need to get total of all the parent components (MyComponent2 + SubView1 + SubView2).

示例

export default class App extends Component {
  state = {
    position: 0,
  };
  _onLayout = ({ nativeEvent: { layout: { x, y, width, height } } }) => {
    this.setState(prevState => ({
      position: prevState.position + y
    }));
  };
  componentDidMount() {
    setTimeout(() => {
      // This will scroll the view to SubView2
      this.scrollView.scrollTo({x: 0, y: this.state.position, animated: true})
    }, 5000);
  }
  render() {
    return (
      <ScrollView style={styles.container} ref={(ref) => this.scrollView = ref}>
        <View style={styles.view}>
          <Text>{'MyComponent1'}</Text>
        </View>
        <View style={[styles.view, { backgroundColor: 'blue'}]} onLayout={this._onLayout}>
          <Text>{'MyComponent2'}</Text>
          <View style={[styles.view, , { backgroundColor: 'green'}]} onLayout={this._onLayout}>
            <Text>{'SubView1'}</Text>
            <View style={[styles.view, { backgroundColor: 'yellow'}]} onLayout={this._onLayout}>
              <Text>{'SubView2'}</Text>
            </View>
          </View>
        </View>
      </ScrollView>
    );
  }
} 

这篇关于React Native - 如何从ScrollView获取Y偏移值视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆