可以在 React Native 中检测到视图组件的触摸吗? [英] Can touch out side a View Component be detected in react native?

查看:28
本文介绍了可以在 React Native 中检测到视图组件的触摸吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 React 原生应用程序屏幕有视图组件,文本输入很少.如何在该视图之外的屏幕上检测到触摸?请帮忙.

My React native application screen has View component with few text inputs. How can touch be detected on screen outside that View? Please help.

谢谢

推荐答案

正如 Andrew 所说:您可以包装您的视图使用 TouchableWithoutFeedback 并添加一个 onPress,您可以检测到何时点击视图.

As Andrew said: You can wrap your View with TouchableWithoutFeedback and adding a onPress you can detect when the view is tapped.

另一种实现方式是对来自视图的触摸事件做出响应.

Another way to achieve that is having responses for touch events from the view.

 /* Methods that handled the events */
handlePressIn(event) {
  // Do stuff when the view is touched
}

handlePressOut(event) {
    // Do stuff when the the touch event is finished
}

...

    <View
      onStartShouldSetResponder={(evt) => true}
      onMoveShouldSetResponder={(evt) => true}
      onResponderGrant={this.handlePressIn}
      onResponderMove={this.handlePressIn}
      onResponderRelease={this.handlePressOut}
    >
         ...
    </View>

Grant 和 move 的区别在于 Grant 只是在用户按下时,而 Move 是在用户按下并移动按下的位置时

The difference between Grant and move is that Grant is just when the user press, and Move is when the user is pressing and moving the position of the press

这篇关于可以在 React Native 中检测到视图组件的触摸吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆