React Native Get Clicked Location of Touchscreen Using PanResponder

PanResponder

To get the clicked location of touchscreen React Native provides an easy to use the component called PanResponder. it is used to recognize the touch on both Android and iOS mobile phone screens. This example is for those who want to start the gaming application development in React Native.

To Import PanResponder in Code

import { PanResponder} from 'react-native'

Use of PanResponder

this.panResponder = PanResponder.create({
    onStartShouldSetPanResponder: (event, gestureState) => true,
    onStartShouldSetPanResponderCapture: (event, gestureState) => true,
    onMoveShouldSetPanResponder: (event, gestureState) => false,
    onMoveShouldSetPanResponderCapture: (event, gestureState) => false,
    onPanResponderGrant: (event, gestureState) => false,
    onPanResponderMove: (event, gestureState) => false,
    onPanResponderRelease: (event, gestureState) =>{
        this.setState({
            locationX: event.nativeEvent.locationX.toFixed(2),
            locationY: event.nativeEvent.locationY.toFixed(2)
       });
    }
});

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Code

Open App.js in any code editor and replace the code with the following code

App.js

//This is an example code to Get the Clicked Position of Touch Screen Using PanResponder//
import React, { Component } from 'react';
//import react in our code.
import { StyleSheet, View, Platform, Text, PanResponder } from 'react-native';
//import all the components we are going to use.

export default class Project extends Component {
  constructor() {
    super();
    //initialize state
    this.panResponder;
    this.state = {
      locationX: 0,
      locationY: 0,
    };
    //panResponder initialization
    this.panResponder = PanResponder.create({
      onStartShouldSetPanResponder: (event, gestureState) => true,
      onStartShouldSetPanResponderCapture: (event, gestureState) => true,
      onMoveShouldSetPanResponder: (event, gestureState) => false,
      onMoveShouldSetPanResponderCapture: (event, gestureState) => false,
      onPanResponderGrant: (event, gestureState) => false,
      onPanResponderMove: (event, gestureState) => false,
      onPanResponderRelease: (event, gestureState) => {
        this.setState({
          //After the change in the location 
          //state will be upated to re-render the screen and show the location in view
          locationX: event.nativeEvent.locationX.toFixed(2),
          locationY: event.nativeEvent.locationY.toFixed(2),
        });
      },
    });
  }
  render() {
    return (
      <View style={styles.MainContainer}>
        <View style={styles.childView}>
        {/*Text to show the screen location we get*/}
          <Text
            style={[
              {
                top: parseFloat(this.state.locationY - 15),
                left: parseFloat(this.state.locationX - 15),
              },
            ]}>
            X: {this.state.locationX}, Y: {this.state.locationY}
          </Text>
          {/*View to show green dot where user touched*/}
          <View
            style={[
              styles.point,
              {
                top: parseFloat(this.state.locationY - 2),
                left: parseFloat(this.state.locationX - 11),
              },
            ]}
          />
          {/*We will get the position of the touch on below View*/}
          <View
            style={{ flex: 1, backgroundColor: 'transparent' }}
            {...this.panResponder.panHandlers}
          />
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  MainContainer: {
    flex: 1,
    paddingTop: Platform.OS === 'ios' ? 40 : 40,
  },
  childView: {
    flex: 1,
    overflow: 'hidden',
  },
  point: {
    height: 22,
    width: 22,
    marginTop: 5,
    position: 'absolute',
    borderRadius: 14,
    backgroundColor: '#00FF30',
  },
});

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Output Screenshots

Output in Online Emulator

This is how you can get clicked on the location of the touchscreen. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂


Related Posts

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.