Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native detect end of long press

My problem is pretty simple, i'm trying to detect the end of a onLongPress event. Basicly when the user release the press.

I'v try every event possible in TouchableWithoutFeedback but there is only one event which is trigger at a time.

import React from 'react'

import {
	View,
	Text,
	Dimensions,
	CameraRoll
} from 'react-native'

import Camera from 'react-native-camera';

const { width, height } = Dimensions.get('window')
class ImageBrowser extends React.Component {
	static navigationOptions = {
		title: 'Unsplash Images',
	}

	state = {
		images: [],
		loading: true,
		page: 1,
		isRecording : false
	}

	takeVideo(){
		this._recordVideo.bind(this);
	}

	_recordVideo(){
		this.camera.capture({mode: Camera.constants.CaptureMode.video})
			.then((data) => {
				console.log(data);
			})
			.catch((err) => {
				console.log(err)
			})
	}

	_stopRecord(){
		this.camera.stopCapture();
	}

	render() {
		return (
			<View style={{flex: 1}}>
				<Camera
					ref={(cam) => {this.camera = cam;}}
					style={styles.preview}
					aspect={Camera.constants.Aspect.fill}
					type={Camera.constants.Type.front}
				>
					<Text style={styles.capture} onLongPress={this.takeVideo.bind(this)} onPress={this._stopRecord.bind(this)} onPressOut={this._stopRecord.bind(this)}>[CAPTURE]</Text>
				</Camera>
			</View>
		)
	}
}

const styles = {
	preview: {
		flex: 1,
		justifyContent: 'flex-end',
		alignItems: 'center',
		height: Dimensions.get('window').height,
		width: Dimensions.get('window').width
	},
	capture: {
		flex: 0,
		backgroundColor: '#fff',
		borderRadius: 5,
		color: '#000',
		padding: 10,
		margin: 40
	}
}

export default ImageBrowser
like image 553
Ethrak Avatar asked Jun 08 '17 19:06

Ethrak


People also ask

How do you handle the long press in React Native?

These long presses can be handled by passing a function to the onLongPress props of any of the "Touchable" components.

What is difference between TouchableOpacity and TouchableHighlight?

TouchableOpacity increases the lighteness of a button when tocuhed while TouchableHighlight increases the darkness of a button when touched.

What is hitSlop?

hitSlop ​ This defines how far your touch can start away from the button. This is added to pressRetentionOffset when moving off of the button.

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.


1 Answers

Use Touch*** Component

onPressIn

onPressOut

http://facebook.github.io/react-native/docs/touchablewithoutfeedback.html

like image 89
yongqian_iOS Avatar answered Oct 24 '22 13:10

yongqian_iOS