Javascript React Native-如何从ScrollView获取视图的Y偏移值?

Javascript React Native-如何从ScrollView获取视图的Y偏移值?,javascript,react-native,uiscrollview,scrollview,Javascript,React Native,Uiscrollview,Scrollview,我正在尝试获取视图的滚动位置。但是Y偏移到页面的值,该值与视图的位置无关 滚动视图层次结构: <ScrollView> - MyComponent1 - MyComponent2 - SubView1 - SubView2 - <View> (Added ref to this view and passing Y offset value through props) - MyComponent3 </Scro

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

滚动视图层次结构:

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

-真菌组分1
-真菌组分2
-子视图1
-子视图2
-(将ref添加到此视图并通过道具传递Y偏移值)
-真菌组分3
子视图2组件:

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; }}>
this.myComponent.measure((fx,fy,width,height,px,py)=>{
console.log('组件宽度为:'+宽度)
console.log('组件高度为:'+高度)
console.log('X到帧的偏移量:'+fx)
console.log('Y到帧的偏移量:'+fy)
console.log('X页偏移量:'+px)
console.log('Y到页面的偏移量:'+py)
此.props.moveScrollToParticularView(py)
})
{this.myComponent=view;}}>
我已经在
onScroll
方法上检查了
子视图2
视图的确切位置。但与
测量值
匹配。我可以看出
测量值
是错误的


是ScrollView层次结构问题吗?

View
组件有一个名为的属性。可以使用此属性获取该组件的位置

仅限布局

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

{nativeEvent: { layout: {x, y, width, height}}}
计算布局后立即触发此事件, 但新的布局可能还没有反映在当时的屏幕上 收到事件,尤其是在布局动画处于活动状态时 进步

更新

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>
    );
  }
} 
onLayout
prop为父组件提供位置。这意味着要找到
子视图2
的位置,需要获得所有父组件的总数(
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>
    );
  }
} 
导出默认类应用程序扩展组件{
状态={
位置:0,
};
_onLayout=({nativeEvent:{layout:{x,y,width,height}}})=>{
this.setState(prevState=>({
位置:prevState.position+y
}));
};
componentDidMount(){
设置超时(()=>{
//这会将视图滚动到子视图2
this.scrollView.scrollTo({x:0,y:this.state.position,动画:true})
}, 5000);
}
render(){
返回(
this.scrollView=ref}>
{'MyComponent1'}
{'myComponent 2'}
{'SubView1'}
{'SubView2'}
);
}
} 

Ya。还尝试了使用
onLayout
。实际上
Y值
对于
子视图2
为0。如果我在MyComponent2上添加ref,我将得到Y值。我想需要加上每个家长。让我试试这个。