【问题标题】:How do you detect if the right half of the screen has been tapped in react-native?你如何检测是否在 react-native 中点击了屏幕的右半部分?
【发布时间】:2015-07-25 13:47:25
【问题描述】:

原本我打算创建一个大小为屏幕一半大小的视图组件,并将其包裹在TouchableHighlight 中,但这看起来很乱。

【问题讨论】:

    标签: ios reactjs react-native


    【解决方案1】:

    看看 Gesture Responder 系统,它可以让你设置一个视图来对触摸做出反应:

    http://facebook.github.io/react-native/docs/gesture-responder-system.html#content

    具体来说,如果您向视图传递 onStartShouldSetResponder 属性,它是一个返回 true 的函数。然后,您还可以将 onResponderGrant 函数作为 prop 传递,该函数将接收包含您需要的详细信息的事件对象。

    【讨论】:

      【解决方案2】:

      修复它。

      我为元素添加了一个包装器<View {...this.panResponder.panHandlers}>,并填充了onPanResponderGrant函数:

          onPanResponderGrant: ({ nativeEvent: { touches } }, { x0, y0, moveX }) =>{
      
          // if on right side of screen
          if (x0 > (Dimensions.get('window').width / 2)){
            _this.nextPhoto();
          } 
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-17
        • 1970-01-01
        • 1970-01-01
        • 2019-02-22
        • 2018-07-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多