2021-11-09 React-Native PanResponder描述

最近想封装一个滑动组件给项目其他组件使用,React Native官方文档中就PanResponder的使用有做大致的讲解,同时网上也有各种关于PanResponder的详解教程,但大多只是官网的一个摘抄,并且就响应事件(onStartShouldSetPanResponder、onStartShouldSetPanResponderCapture、onMoveShouldSetPanResponder、onMoveShouldSetPanResponderCapture)的参数如何配置并没有详细解答,导致开始接触的时候对于React Native的事件传递和响应比较模糊。本次希望各封装一个Hoc和Hook组件,来实现手势滑动,且同时不影响被包裹的子组件事件响应,因此进行这次的PanResponder探究

重点结论

不能在TouchableOpacity中使用触摸

被设置了PanResponder的View,如果内部有TouchableOpacity,只有当onStartShouldSetPanResponderCapture为false时,内部的事件响应才能被执行

详细分析

决定view的事件响应主要由四个函数的返回值决定:

onStartShouldSetPanResponder:确定是否在view组在手指按下的时候响应touch事件

onStartShouldSetPanResponderCapture:确定是否在view组件被按下的时候响应touch事件后,是否阻止事件冒泡(它的子组件的事件将不被响应)

onStartShouldSetPanResponder:确定是否在view组件手指移动的时候响应touch事件

onMoveShouldSetPanResponderCapture:确定是否在view组件手指移动的时候响应touch事件后,是否阻止事件冒泡(它的子组件的事件将不被响应)

onStartShouldSetPanResponder: boolean,

onStartShouldSetPanResponderCapture: boolean,

onMoveShouldSetPanResponder: boolean,

onMoveShouldSetPanResponderCapture: boolean,

项目配置方式

将事件全部交给PanResponder来处理,子组件中不需要响应事件,则可以将以上返回全部配置成ture

子组件中有自己的事件需要响应,则必须将onStartShouldSetPanResponderCapture配置成false,其他根据自己的需求进行配置

我们项目的需求大体是,只希望PanResponder处理move事件,子组件中有自己的onPress事件响应,因此,我们的项目配置如下:

// 要求成为手势响应者,且不阻止点击事件冒泡(onStartShouldSetPanResponderCapture-false)

  onStartShouldSetPanResponder: () => true,

      onStartShouldSetPanResponderCapture: () => false,

      onMoveShouldSetPanResponder: () => true,

      onMoveShouldSetPanResponderCapture: () => true,

探究过程

我们在代码中将这4个返回值设置为state状态值,通过修改成true或false,来进行不同的组合。同时,在View组件内部添加TouchableOpacity并设置onPress来响应点击事件(如果对touch事件感兴趣的同学可以里面在嵌套一个View同时设置PanResponder,我只是基于项目目前的场景来设定onPress来先满足需要,原理其实通过此文是可以一并探究的)

文末会贴上我们的demo全量代码

重要代码

constructor(props: any) {

    super(props)

    this.state = {

      hint: '',

      onStartShouldSetPanResponder: false,

      onStartShouldSetPanResponderCapture: false,

      onMoveShouldSetPanResponder: false,

      onMoveShouldSetPanResponderCapture: false,

    }

    // this.createPanResponder()

    this._panResponder = PanResponder.create({

      // 要求成为响应者:

      onStartShouldSetPanResponder: () => this.state.onStartShouldSetPanResponder,

      onStartShouldSetPanResponderCapture: () => this.state.onStartShouldSetPanResponderCapture,

      onMoveShouldSetPanResponder: () => this.state.onMoveShouldSetPanResponder,

      onMoveShouldSetPanResponderCapture: () => this.state.onMoveShouldSetPanResponderCapture,

      onPanResponderGrant: (evt, gestureState) => {

        this.log('onPanResponderGrant  ---  事件响应、开始')

        // 开始手势操作。给用户一些视觉反馈,让他们知道发生了什么事情!

      },

      onPanResponderMove: (evt, gestureState) => {

        this.log('onPanResponderMove  ----  滑动')

      },

      onPanResponderTerminationRequest: (evt, gestureState) => true,

      onPanResponderRelease: (evt, gestureState) => {

        // 用户放开了所有的触摸点,且此时视图已经成为了响应者。

        // 一般来说这意味着一个手势操作已经成功完成。

        this.log('onPanResponderRelease  ----  事件结束')

      },

      onPanResponderTerminate: (evt, gestureState) => {

        // 另一个组件已经成为了新的响应者,所以当前手势将被取消。

        this.log('onPanResponderTerminate  ---- 被取消了')

      },

    })

  }

    //其他代码段

render() {

    return (

      <View style={{flex: 1,

        padding: 10,}}>

        <View>

        <View

          style={PanResponderDemoStyle.contain}

          {...this._panResponder.panHandlers}

        >

          <TouchableOpacity

            style={PanResponderDemoStyle.touchView}

            onPress={()=>this.log('onPress  ---  按钮事件')}

          ><Text>TouchableOpacity按钮</Text></TouchableOpacity>

          <Text>事件响应日志:</Text>

          <Text>

            {this.state.hint}

          </Text>

        </View>

      </View>

    )

  }

组合效果

名称定义:

TouchableOpacity组件:被设定了PanResponder的View包裹的可以点击的子组件

View组件:不包含TouchableOpacity组件的View中的其他区域

touch事件:包含onPanResponderGrant和onPanResponderRelease的回调响应

move事件:指onPanResponderMove的回调响应

注意问题

不管onStartShouldSetPanResponder等true或false如何设置,View组件都会响应touch和move事件,以下主要关注TouchableOpacity组件的事件响应情况(通过这个对比,可以解决通过Hoc将这个手势功能添加给任意一个字组件中遇到的事件响应问题)

onStartShouldSetPanResponder为true,其他为false 

TouchableOpacity组件内部点击会响应onPress事件,不响应touch和move事件,就算在TouchableOpacity组件内移动手指,在弹起后,依旧会响应onPress事件 

onStartShouldSetPanResponderCapture为true,其他为false

1.TouchableOpacity组件内部点击不会响应onPress事件,只会响应touch和move事件的

onMoveShouldSetPanResponder为true,其他为false 

1.TouchableOpacity组件如果只点击不滑动,在手指离开的时候会响应onPress事件并且不会响应touch事件, 

2.TouchableOpacity组件手指按下后并滑动,则会响应touch和move事件 


onMoveShouldSetPanResponderCapture为true,其他为false

TouchableOpacity只点击不滑动,在手指离开的时候会响应onPress事件,但不响应touch事件

TouchableOpacity组件手指按下后滑动,不响应onPress事件,同时会响应touch和move事件

TouchableOpacity只点击不滑动,会触发onLongPress事件,并且此时再滑动,依旧会触发touch和move事件

View组件点击不触发touch事件


Demo全量代码

import React from 'react'

import {

  View,

  Text,

  StyleSheet, PanResponderInstance, PanResponder, TouchableOpacity,

} from 'react-native'

export class PanResponderDemoView extends React.Component<any, {

  hint: string,

  onStartShouldSetPanResponder: boolean,

  onStartShouldSetPanResponderCapture: boolean,

  onMoveShouldSetPanResponder: boolean,

  onMoveShouldSetPanResponderCapture: boolean,

}> {

  _panResponder: PanResponderInstance

  constructor(props: any) {

    super(props)

    this.state = {

      hint: '',

      onStartShouldSetPanResponder: false,

      onStartShouldSetPanResponderCapture: false,

      onMoveShouldSetPanResponder: false,

      onMoveShouldSetPanResponderCapture: false,

    }

    // this.createPanResponder()

    this._panResponder = PanResponder.create({

      // 要求成为响应者:

      onStartShouldSetPanResponder: () => this.state.onStartShouldSetPanResponder,

      onStartShouldSetPanResponderCapture: () => this.state.onStartShouldSetPanResponderCapture,

      onMoveShouldSetPanResponder: () => this.state.onMoveShouldSetPanResponder,

      onMoveShouldSetPanResponderCapture: () => this.state.onMoveShouldSetPanResponderCapture,

      onPanResponderGrant: (evt, gestureState) => {

        this.log('onPanResponderGrant  ---  事件响应、开始')

        // 开始手势操作。给用户一些视觉反馈,让他们知道发生了什么事情!

      },

      onPanResponderMove: (evt, gestureState) => {

        this.log('onPanResponderMove  ----  滑动')

      },

      onPanResponderTerminationRequest: (evt, gestureState) => true,

      onPanResponderRelease: (evt, gestureState) => {

        // 用户放开了所有的触摸点,且此时视图已经成为了响应者。

        // 一般来说这意味着一个手势操作已经成功完成。

        this.log('onPanResponderRelease  ----  事件结束')

      },

      onPanResponderTerminate: (evt, gestureState) => {

        // 另一个组件已经成为了新的响应者,所以当前手势将被取消。

        this.log('onPanResponderTerminate  ---- 被取消了')

      },

    })

  }

  log(text: string) {

    console.log(text, this.state.hint)

    this.setState({

      hint: this.state.hint + '\n' + text,

    })

  }

  render() {

    return (

      <View style={{flex: 1,

        padding: 10,}}>

        <View>

          <TouchableOpacity

            style={PanResponderDemoStyle.touchView}

            onPress={()=>this.log('onPress  ---  按钮事件')}

            onLongPress={()=>this.log('onLongPress  ---  按钮长按事件')}

          ><Text>TouchableOpacity按钮</Text></TouchableOpacity>

          <Text onPress={()=>this.setState({hint:''})}>清空日志</Text>

          <StateCell

            name={'onStartShouldSetPanResponder'} select={this.state.onStartShouldSetPanResponder}

            onPress={() => this.setState({onStartShouldSetPanResponder: !this.state.onStartShouldSetPanResponder})}/>

          <StateCell

            name={'onStartShouldSetPanResponderCapture'} select={this.state.onStartShouldSetPanResponderCapture}

            onPress={() => this.setState({onStartShouldSetPanResponderCapture: !this.state.onStartShouldSetPanResponderCapture})}/>

          <StateCell

            name={'onMoveShouldSetPanResponder'} select={this.state.onMoveShouldSetPanResponder}

            onPress={() => this.setState({onMoveShouldSetPanResponder: !this.state.onMoveShouldSetPanResponder})}/>

          <StateCell

            name={'onMoveShouldSetPanResponderCapture'} select={this.state.onMoveShouldSetPanResponderCapture}

            onPress={() => this.setState({onMoveShouldSetPanResponderCapture: !this.state.onMoveShouldSetPanResponderCapture})}/>

        </View>

        <View

          style={PanResponderDemoStyle.contain}

          {...this._panResponder.panHandlers}

        >

          <TouchableOpacity

            style={PanResponderDemoStyle.touchView}

            onPress={()=>this.log('onPress  ---  按钮事件')}

            onLongPress={()=>this.log('onLongPress  ---  按钮长按事件')}

          ><Text>TouchableOpacity按钮</Text></TouchableOpacity>

          <Text>事件响应日志:</Text>

          <Text>

            {this.state.hint}

          </Text>

        </View>

      </View>

    )

  }

}

class StateCell extends React.PureComponent<{ name: string, select: boolean, onPress: (() => void) }> {

  get selectText() {

    return this.props.select ? 'true' : 'false'

  }

  render() {

    const {name, select} = this.props

    return (

      <View style={{

        flexDirection: 'row',

        justifyContent: 'space-between',

      }}>

        <Text>{`${name}:`}</Text>

        <View style={{

          flexDirection: 'row',

          justifyContent:'flex-end',

          width: 120,

        }}>

          <Text style={{color: select ? 'red' : '#666',marginRight:4}}>{this.selectText}</Text>

          <Text style={{color: '#18ABFB', fontSize: 14}} onPress={() => this.props.onPress()}>{'修改'}</Text></View>

      </View>

    )

  }

}

const PanResponderDemoStyle = StyleSheet.create({

  contain: {

    flex: 1,

    paddingTop: 10,

    alignItems: 'center',

    backgroundColor: '#F2F2F2',

  },

  touchView: {

    width: 300,

    height: 100,

    justifyContent: 'center',

    alignItems: 'center',

    backgroundColor: 'green',

  },

})

————————————————

版权声明:本文为CSDN博主「rock.dai」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/u010899138/article/details/111322422

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容