我需要的是当我触摸并按住一个按钮时,我也应该能够触摸按钮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>
基本上,我有一个屏幕,我可以通过点击并按住录制按钮(按钮1)来录制视频。在同一个屏幕上,我有一个翻盖相机按钮(按钮2)。我希望在录制视频时能够点击翻盖相机按钮。
答案 0 :(得分:1)
这是我的多个按钮解决方案
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>
);
}
}
然后我只需用组件
包裹需要同时按下的按钮<MultiTouch style={this.style.view}>
<UpDownButton />
<UpDownButton />
</MultiTouch>
干杯!
<强>更新强>
由于本机react v.0.51中的更改中断,我以前的解决方案不再起作用。但我设法创造一个新的。我没有使用TouchableWithoutFeedback和onPress,而是在每个应该有多点触控的按钮上使用View和onTouch。
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>
);
}
}
答案 1 :(得分:0)
使用onTouchStart,onTouchEnd视图组件的道具可以轻松解决此问题,而无需使用手势响应方法。
因此修改后的代码看起来像
<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>
答案 2 :(得分:0)
我使用了react-native-gesture-handler。安装并替换
import { TouchableOpacity } from 'react-native';
使用
import { TouchableOpacity } from 'react-native-gesture-handler';
示例
<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>
链接:https://software-mansion.github.io/react-native-gesture-handler/docs/component-touchables.html
该库还提供了可以直接使用的按钮组件,而不是使用TouchableOpacity包装文本
答案 3 :(得分:0)
尝试:
import { TouchableOpacity } from 'react-native';
代替:
import { TouchableOpacity } from 'react-native-gesture-handler';
将帮助您使用多个按钮。
例如,如果您在TouchableOpacity
中有一个TouchableWithoutFeedback
,则当TouchableOpacity is touched
时,它将仅调用TouchableOpacity的onPress,而不会调用TouchableWithoutFeedback
的onPress。