[英]How to trigger another Touchable component's press event by tap a touchable component?
How to trigger another Touchable component's press event by tap a touchable component?如何通过点击可触摸组件来触发另一个可触摸组件的按下事件? thanks!
谢谢!
Example:-例子:-
<TouchableHighlight onPress={this._triggerSecond}>
<Image
style={styles.button}
source={require('./myButton.png')}
/>
</TouchableHighlight>
If user will tap on first TouchableHighlight then it should trigger second TouchableHighlight.如果用户点击第一个 TouchableHighlight,那么它应该触发第二个 TouchableHighlight。
<TouchableHighlight>
<Image
style={styles.button}
source={require('./myButton.png')}
/>
</TouchableHighlight>
For those who still need to fire a press programmatically, I found a solution.对于那些仍然需要以编程方式触发印刷机的人,我找到了一个解决方案。 First you need to bind your touchable to
this
by using ref :首先,您需要使用 ref 将您的可触摸对象绑定到
this
:
<TouchableOpacity
onPress={() => alert("click")}
ref={(touchable) => this._touchable = touchable}
>
then when you want to launch onPress without clicking it just call然后当你想在不点击的情况下启动 onPress 时,只需调用
this._touchable.touchableHandlePress()
Well I guess instead of thinking in that way I guess you can thinking this as just another TouchableHighlight
calling the same method such as:好吧,我想而不是那样想,我想您可以将其视为调用相同方法的另一个
TouchableHighlight
,例如:
// First one
<TouchableHighlight onPress={this._method}>
<Image
style={styles.button}
source={require('./myButton.png')}
/>
</TouchableHighlight>
// Second one
<TouchableHighlight onPress={this._method}>
<Image
style={styles.button}
source={require('./myButton.png')}
/>
</TouchableHighlight>
In this way both components when tap are going to invoke to the same method
.这样,当 tap 时两个组件将调用相同的
method
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.