Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable touch on multiple buttons simultaneously in react native?

I need that when I am touching and holding one button then I should also be able to touch on the button 1.

<View>
  
  <View 
  onStartShouldSetResponder={()=>this.console("Button 2 Clicked")}>
    <Text>BUTTON 2</Text>
  </View>
  
  <TouchableOpacity 
  onPressIn={()=>this.console('Button 1 pressed')}
  onPressOut={()=>this.console('Button 1 released')}>
    <View>
      <Text>BUTTON 1</Text>
    </View>
  </TouchableOpacity>

</View>

Basically, I have a screen where I can record a video by tapping and holding the record button(Button 1). On the same screen, I have a flip camera button (Button 2). I want that I should be able to click on the flip camera button while I am recording the video.

like image 274
Satya P. Goyal Avatar asked Mar 14 '17 01:03

Satya P. Goyal


People also ask

How do you toggle multiple buttons in react?

Onclick function execute the myChangeHandler , which changes the state to opposite on every click. This will toggle the content inside h1 element. Here the function execute the change for both button.

How do you use two buttons side by side react native?

create({ container: { flex: 1, }, parent: { flex: 1, flexDirection: "row", justifyContent: "space-around", }, }); export default App; Here, we are adding two CButton with different text values. The wrapper View of these buttons is having flex = 1 with flexDirection = row and justifyContent as space-around.

How do you increase touchable area react native?

You can use the View#hitSlop property to increase the touchable area. This can be useful in scenarios where you know that the increased touch area won't overlap with other touchables. A more robust solution is to use the padding style property.

Can I Build my Own buttons in React Native?

If the basic button doesn't look right for your app, you can build your own button using any of the "Touchable" components provided by React Native. The "Touchable" components provide the capability to capture tapping gestures, and can display feedback when a gesture is recognized.

What is React-Native-platform touchable?

React Native Touchable is a component to overcome the limitation of the styling of button component. There are four types of Touchables. Which are. After exploring this example if you want to explore more then you can also see React Native Touchable using react-native-platform-touchable.

How many types of React Native touchable are there?

That was the 4 Different Type of React Native Touchable provided by React Native Core library. If you want to explore more then you can see React Native Platform Touchable which is a built-in wrapper over the various React Native Touchable. First of all why we need that?

How to make a react native app with npm?

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


4 Answers

Try:

import { TouchableOpacity } from 'react-native';

Instead of:

import { TouchableOpacity } from 'react-native-gesture-handler';

Will help you to multiple buttons.

For example, if you have a TouchableOpacity inside a TouchableWithoutFeedback, when TouchableOpacity is touched, it will only call TouchableOpacity's onPress, and will not be called onPress of TouchableWithoutFeedback.

like image 165
Doan Bui Avatar answered Oct 21 '22 18:10

Doan Bui


I used react-native-gesture-handler. Install it and just replace

import { TouchableOpacity } from 'react-native';

with

import { TouchableOpacity } from 'react-native-gesture-handler';

Example

<View>

  <TouchableOpacity 
  onPressIn={()=>this.console('Button 2 pressed')}
  onPressOut={()=>this.console('Button 2 released')}>
    <View>
      <Text>BUTTON 2</Text>
    </View>
  </TouchableOpacity>

  <TouchableOpacity 
  onPressIn={()=>this.console('Button 1 pressed')}
  onPressOut={()=>this.console('Button 1 released')}>
    <View>
      <Text>BUTTON 1</Text>
    </View>
  </TouchableOpacity>

</View>

Link: https://software-mansion.github.io/react-native-gesture-handler/docs/component-touchables.html

This library also offers button components which can be directly used instead of wrapping Text with TouchableOpacity

like image 40
Ganesh Sundar C Avatar answered Oct 21 '22 19:10

Ganesh Sundar C


This problem can easily be resolved using onTouchStart, onTouchEnd props of View component without using gesture responder methods.

So the modified code will look like

<View>

  <View onTouchStart={()=>this.console("Button 2 Clicked")}>
    <Text>BUTTON 2</Text>
  </View>

  <View 
    onTouchStart={()=>this.console('Button 1 pressed')}
    onTouchEnd={()=>this.console('Button 1 released')}>
      <Text>BUTTON 1</Text>
  </View>

</View>
like image 29
Satya P. Goyal Avatar answered Oct 21 '22 17:10

Satya P. Goyal


This is my solution for multiple buttons

import React, { Component } from 'react';
import {
    View,
    PanResponder,
} from 'react-native';
import ReactNativeComponentTree from'react-native/Libraries/Renderer/shims/ReactNativeComponentTree';

export default class MultiTouch extends Component{
    constructor(props) {
        super(props);

        this.onTouchStart = this.onTouchStart.bind(this);
        this.onTouchEnd = this.onTouchEnd.bind(this);
        this.onTouchCancel = this.onTouchCancel.bind(this);

        this.triggerEvent = this.triggerEvent.bind(this);
    }
    onTouchStart(event){
        const element = ReactNativeComponentTree.getInstanceFromNode(event.target)._currentElement;
        this.triggerEvent(element._owner, 'onPressIn');
    }
    onTouchEnd(event){
        const element = ReactNativeComponentTree.getInstanceFromNode(event.target)._currentElement;
        this.triggerEvent(element._owner, 'onPressOut');
    }
    onTouchCancel(event){
        const element = ReactNativeComponentTree.getInstanceFromNode(event.target)._currentElement;
        this.triggerEvent(element._owner, 'onPressOut');
    }
    onTouchMove(event){
       // console.log(event);
    }
    triggerEvent(owner, event){ // Searching down the 
        if(!owner || !owner.hasOwnProperty('_instance')){
            return;
        }
        if(owner._instance.hasOwnProperty(event)){
            owner._instance[event]();
        }else{
            this.triggerEvent(owner._currentElement._owner, event);
        }
    }
    render(){
        return (
            <View
                onTouchStart={this.onTouchStart}
                onTouchEnd={this.onTouchEnd}
                onTouchCancel={this.onTouchCancel}
                onTouchMove={this.onTouchMove}>
                {this.props.children}
            </View>
        );
    }
}

Then I simply wrap the buttons that needs to be pressed at the same time withe the component

<MultiTouch style={this.style.view}>
    <UpDownButton />
    <UpDownButton />
</MultiTouch>

Cheers!

UPDATE

Because of breaking changes in native react v.0.51, my previous solution does not work any more. But I manage to create a new one. Instead of using TouchableWithoutFeedback and onPress I use View and onTouch on each button that should have multitouch.

import React, { Component } from 'react';
import {
    View,
} from 'react-native';
export default class RoundButtonPart extends Component{
    constructor(props) {
        super(props);

        this.state = { active: false };

        this.onTouchStart = this.onTouchStart.bind(this);
        this.onTouchEnd = this.onTouchEnd.bind(this);
        this.onTouchCancel = this.onTouchCancel.bind(this);
    }

    onTouchStart(event){
        this.setState({ active: true });
        this.props.onPressIn && this.props.onPressIn();
    }
    onTouchEnd(event){
        this.setState({ active: false });
        this.props.onPressOut && this.props.onPressOut();
    }
    onTouchCancel(event){
        this.setState({ active: false });
        this.props.onPressOut && this.props.onPressOut();
    }
    onTouchMove(event){

    }
    render(){
        return (
            <View
                onTouchStart={this.onTouchStart}
                onTouchEnd={this.onTouchEnd}
                onTouchCancel={this.onTouchCancel}
                onTouchMove={this.onTouchMove}>

                 {this.props.children}
            </View>
        );
    }
}
like image 42
FOLOF Avatar answered Oct 21 '22 17:10

FOLOF