[技术博客]利用第三方框架react-native-swipeout实现左右滑动出现按钮

在之前的开发中,为了实现用户不同手势操作能够对应不同的功能,我们考虑使用React-Native的API——PanResponder,实现识别用户的手势,实现不同的功能。但我们很快就发现,这样简单的实现,无任何反馈的话,用户很难知道具有这样的功能。因此,我们准备实现类似手机QQ消息界面的左滑出现几个按钮。使用react-native的第三方框架react-native-swipeout可以很简单的实现此功能。

安装react-native-swipeout

框架的github地址: react-native-swipeout

可以使用npm install --save react-native-swipeoutyarn add react-native-swipeout命令安装框架

框架的使用

在框架的github项目中,开发者给出如下的示例代码

import Swipeout from 'react-native-swipeout';

// Buttons
var swipeoutBtns = [
  {
    text: 'Button'
  }
]

// Swipeout component
<Swipeout right={swipeoutBtns}>
  <View>
    <Text>Swipe me left</Text>
  </View>
</Swipeout>

阅读框架github项目中的文档,我们可以知道框架中实现了Swipeout组件,具有以下属性(props)

PropTypeOptionalDefaultDescription
autoCloseboolYesfalse是否会自动关闭按钮列表
backgroundColorstringYes'#dbddde'背景颜色
closeboolYes当前列是否会关闭按钮
disabledboolYesfalse是否禁用swipeout
leftarrayYes[]右滑时出现在左侧的按钮列表
onOpenfuncYes(sectionID, rowId, direction: string) => void
按钮列表开启会执行的函数
onClosefuncYes(sectionID, rowId, direction: string) => void
按钮列表关闭会执行的函数
rightarrayYes[]左滑时出现在右侧的按钮列表
scrollfuncYesprevent parent scroll
stylestyleYesstyle of the container
sensitivitynumberYes50change the sensitivity of gesture
buttonWidthnumberYeseach button width

left和right属性应为形如[{ text: 'Button' }]的列表,其中支持的属性如下

PropTypeOptionalDefaultDescription
backgroundColorstringYes'#b6bec0'按钮的背景颜色
colorstringYes'#ffffff'字体颜色
componentReactNodeYesnullpass custom component to button
onPressfuncYesnull按下后执行的函数
textstringYes'Click Me'text
typestringYes'default'default, delete, primary, secondary
underlayColorstringYesnull按时按钮背景颜色
disabledboolYesfalse是否禁用此按钮

具体使用代码

_renderItem = (item) => {
    var BtnsLeft = [{ text: '清空', type: 'delete',  onPress: ()=> console.log('清空列表')}];
    var BtnsRight = [{ text: '删除', type: 'delete', onPress: ()=>console.log('删除单行数据')}];

    return(
        <Swipeout
            close={!(this.state.sectionID === 'historylist' && this.state.rowID === Id)}
            right={BtnsRight}
            left={BtnsLeft}
            rowID={Id}
            sectionID='historylist'
            autoClose={true}
            backgroundColor='white'
            onOpen={(sectionId, rowId, direction: string) => {
                this.setState({
                    rowID: rowId,
                    sectionID: sectionId
                });
            }}
            scroll={event => console.log('scroll event') }
          >
        <View style={flatStylesWithAvatar.cell}
        >
        具体内容
        </View>
        </Swipeout>
    )
};

在渲染列表中的单行数据时,左右滑动可以出现不同操作的对应按钮,实现效果如下:

1621184-20190514191333041-549052254.gif

转载于:https://www.cnblogs.com/PureMan6/p/10872165.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值