اذهب إلى المحتوى
  • 0

تحديد موقع عنصر - React Native

Yomna Raouf

السؤال

أعمل على تطبيق و قمت بتنسيق مكون ما حتى يظهر في منتصف الشاشة، ما أريد القيام به الآن هو عرض مكون آخر "مثله" فوقه تمامًا. فنا هي أفضل طريقة للقيام بذلك، مثلًا في jquery يتيح لنا التابع ()offset تحديد موضع العنصر فهل يوجد وسيلة مكافئة لها في React Native.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

هناك طريقتين

الأولى : عن طريق إستخدام ال onLayout event ومن خلاله يمكنك الحصول على: 

  • الطول
  • العرض
  • الإحداثي x 
  • الإحداثي y 

وذلك كالاتي : 

export default class measure extends Component {
  constructor(props) {
    super(props);
    this.state = {
      measurements: {},
    }
  }


  render() {
    return (
      <View>
        <View 
          onLayout={({ layoutEvent}) => {
            this.setState({
              measurements: layoutEvent.layout
            })
          }}
        >
          <Text>Measure Me</Text>
        </View>

        <View>
          <Text>Width: {this.state.measurements.width}</Text>
          <Text>Height: {this.state.measurements.height}</Text>
          <Text>X: {this.state.measurements.x}</Text>
          <Text>Y: {this.state.measurements.y}</Text>
        </View>
      </View>
    );
  }
}

 الطريقة الثانية: بإستخدام (UIManager و findNodeHandle)

وذلك كالتالي: 

import React, { Component } from 'react';
import {
  Text,
  View,
  TouchableOpacity,
  UIManager,
  findNodeHandle
} from 'react-native';

export default class measure extends Component {
  constructor(props) {
    super(props);
    this.state = {
      measurements: {},
    }
  }

  measure() { // الخطوة الثالثة
    UIManager.measure(findNodeHandle(this.view), (x, y, width, height) => {
      this.setState({
        measurements: {
          x,
          y,
          width,
          height
        }
      })
    })
  }

  render() {
    return (
      <View>
        <View 
          ref={ref => this.view = ref} // الخطوة الأولى
        >
          <Text>Measure Me</Text>
        </View>
        <TouchableOpacity onPress={() => this.measure() // الخطوة الثانية}>
          <Text>Measure With UIManager</Text>
        </TouchableOpacity>
        <View>
          <Text>Width: {this.state.measurements.width}</Text>
          <Text>Height: {this.state.measurements.height}</Text>
          <Text>X: {this.state.measurements.x}</Text>
          <Text>Y: {this.state.measurements.y}</Text>
        </View>
      </View>
    );
  }
}

بالتوفيق إن شاء الله.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...