• <small id='VFtDU'></small><noframes id='VFtDU'>

  • <legend id='VFtDU'><style id='VFtDU'><dir id='VFtDU'><q id='VFtDU'></q></dir></style></legend>

        <bdo id='VFtDU'></bdo><ul id='VFtDU'></ul>
    1. <tfoot id='VFtDU'></tfoot>
      <i id='VFtDU'><tr id='VFtDU'><dt id='VFtDU'><q id='VFtDU'><span id='VFtDU'><b id='VFtDU'><form id='VFtDU'><ins id='VFtDU'></ins><ul id='VFtDU'></ul><sub id='VFtDU'></sub></form><legend id='VFtDU'></legend><bdo id='VFtDU'><pre id='VFtDU'><center id='VFtDU'></center></pre></bdo></b><th id='VFtDU'></th></span></q></dt></tr></i><div id='VFtDU'><tfoot id='VFtDU'></tfoot><dl id='VFtDU'><fieldset id='VFtDU'></fieldset></dl></div>

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

        时间:2023-08-07

        1. <small id='V2XYm'></small><noframes id='V2XYm'>

              <bdo id='V2XYm'></bdo><ul id='V2XYm'></ul>
              <i id='V2XYm'><tr id='V2XYm'><dt id='V2XYm'><q id='V2XYm'><span id='V2XYm'><b id='V2XYm'><form id='V2XYm'><ins id='V2XYm'></ins><ul id='V2XYm'></ul><sub id='V2XYm'></sub></form><legend id='V2XYm'></legend><bdo id='V2XYm'><pre id='V2XYm'><center id='V2XYm'></center></pre></bdo></b><th id='V2XYm'></th></span></q></dt></tr></i><div id='V2XYm'><tfoot id='V2XYm'></tfoot><dl id='V2XYm'><fieldset id='V2XYm'></fieldset></dl></div>
                    <tbody id='V2XYm'></tbody>

                  <legend id='V2XYm'><style id='V2XYm'><dir id='V2XYm'><q id='V2XYm'></q></dir></style></legend>
                  <tfoot id='V2XYm'></tfoot>

                  本文介绍了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; }}>
                  

                  我在 onScroll 方法上检查了 SubView2 视图的确切位置.但确实与 measure value 匹配.我可以弄清楚 measure value 是错误的.

                  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 组件有一个名为 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}}}
                  

                  计算完布局后立即触发此事件,但当时新的布局可能还没有体现在屏幕上接收到事件,尤其是在布局动画处于进展.

                  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 属性给父组件一个位置.这意味着要找到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 偏移值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 shift 修饰符从键码中获取键字符(值) 下一篇:以交互模式在 qwebview 中打开 plotly

                  相关文章

                  最新文章

                  <i id='mN00Z'><tr id='mN00Z'><dt id='mN00Z'><q id='mN00Z'><span id='mN00Z'><b id='mN00Z'><form id='mN00Z'><ins id='mN00Z'></ins><ul id='mN00Z'></ul><sub id='mN00Z'></sub></form><legend id='mN00Z'></legend><bdo id='mN00Z'><pre id='mN00Z'><center id='mN00Z'></center></pre></bdo></b><th id='mN00Z'></th></span></q></dt></tr></i><div id='mN00Z'><tfoot id='mN00Z'></tfoot><dl id='mN00Z'><fieldset id='mN00Z'></fieldset></dl></div>

                    <legend id='mN00Z'><style id='mN00Z'><dir id='mN00Z'><q id='mN00Z'></q></dir></style></legend>

                    1. <tfoot id='mN00Z'></tfoot>

                      <small id='mN00Z'></small><noframes id='mN00Z'>

                        <bdo id='mN00Z'></bdo><ul id='mN00Z'></ul>