个人笔记:(很多地方比较累赘,求指正)

需求:项目需要在RN窗口 实现对其拖动和滑动动画,点击滑动;

思路:通过PanResponder监听手势操作。配合Animated实现 不需要第三方包,比较简单。

效果:可手动拖动打开/折叠/关闭。或者点击按钮实现

  

实现:

相关变量:(props.initialHeight为使用窗口是传入的初始高度)

this.animatedViewHeight = new Animated.Value(props.initialHeight)//view高度动画值
this.viewHeight = props.initialHeight//用于判读当前位置做动画
this.showAnimate = false//当前位置折叠/打开
//可以不这么使用
this._onPanResponderGrant = this._onPanResponderGrant.bind(this);//绑定动画开始函数
this._onPanResponderRelease = this._onPanResponderRelease.bind(this);//绑定动画结束函数

布局:

render() {return (<Animated.Viewstyle={{width: deviceInfo.phone_width,height: this.animatedViewHeight,//动态高度实现平滑动画backgroundColor: Colors.white,borderTopLeftRadius: Dimens.dp12,borderTopRightRadius: Dimens.dp12,}}><View{...this.panResponder.panHandlers}>//自己的布局代码</View>//自己的布局代码</Animated.View>);
}

监听和实现:

/*** 手势监听写在componentWillMount中* @type {$TEMPORARY$object<{getInteractionHandle, panHandlers: $TEMPORARY$object<{onMoveShouldSetResponder, onMoveShouldSetResponderCapture, onResponderEnd,   *onResponderGrant, onResponderMove, onResponderReject, onResponderRelease, onResponderStart, onResponderTerminate, onResponderTerminationRequest,  *onStartShouldSetResponder, onStartShouldSetResponderCapture}>}>}*/
this.panResponder = PanResponder.create({/*** 是否监听* @returns {boolean}*/onStartShouldSetPanResponder: () => {return true;},/*** 开始手势*/onPanResponderGrant: this._onPanResponderGrant,/*** 手势移动 通过Animated.event的listener监听事件手势变动数据* gestureState.moveY 手势相对坐标定点的绝对距离 为正数* gestureState.dy 手势相对触摸起始点的相对位置 向下为正,向上为负*/onPanResponderMove: Animated.event([null,{}//dy:this.animatedViewHeight],{listener: (event, gestureState) => {// console.log("onPanResponderMove",this.showAnimate+" "+ gestureState.dy+ " "+gestureState.moveY)let values= (deviceInfo.phone_screen_height +55)- gestureState.moveY this.viewHeight=valuesthis.animatedViewHeight.setValue(values)}}),onPanResponderRelease: this._onPanResponderRelease,
})

手势开始和结束函数

/*** 手势开始,用于实现按压动画等效果* @param e* @param gestureState* @private*/
_onPanResponderGrant(e, gestureState) {
};/*** 手势结束 gestureState中的参数和onPanResponderMove中最后一帧参数一样* @param e* @param gestureState* @private*/
_onPanResponderRelease(e, gestureState) {// console.log("_onPanResponderRelease",gestureState.dy+" "+this.gestureStateDy+" " +this.viewHeight)if(gestureState.dy>=0){//向下滑动if (this.viewHeight < this.props.initialHeight) {//结束位置在折叠线一下 则关闭this.props.close()}else {结束位置在折叠线一上 则折叠this.showAnimate = falsethis.viewAnimateHidden()}}else {//向上滑动if (this.viewHeight < this.props.initialHeight) {//结束位置在折叠线一下 则折叠this.showAnimate = falsethis.viewAnimateHidden()}else {结束位置在折叠线一上 则打开this.showAnimate = truethis.viewAnimateShow()}}
}

使用到的动画函数:

/*** 展开动画*/
viewAnimateShow() {this.viewHeight=deviceInfo.phone_height//更新位置避免拖动错误Animated.timing(this.animatedViewHeight, {toValue: deviceInfo.phone_height,duration: 300,useNativeDriver: false}).start()
}/*** 折叠动画*/
viewAnimateHidden() {this.viewHeight=this.props.initialHeight//更新位置避免拖动错误Animated.timing(this.animatedViewHeight, {toValue: this.props.initialHeight,duration: 300,useNativeDriver: false}).start()
}

rn react native PanResponder手势动画 实现窗口拖动 滑动动画 Animated相关推荐

  1. [RN] React Native 键盘管理 在Android TextInput遮盖,上移等问题解决办法

    [RN] React Native 键盘管理 在Android TextInput遮盖,上移等问题解决办法 参考文章: (1)[RN] React Native 键盘管理 在Android TextI ...

  2. [RN] React Native 实现图片预览

    [RN] React Native 实现图片预览 效果预览: 代码如下: 'use strict'; import React, {Component} from 'react'; import {I ...

  3. RN(React Native)

    RN是React的原生组件库 网址:React Native 中文网 · 使用React来编写原生应用的框架 一.ReactNative开发环境的搭建 在本机搭建RN项目开发运行环境 -- 3GB+ ...

  4. (笔记) RN React Native 热更新(react-native-code-push) AppCenter

    苹果App允许使用热更新Apple's developer agreement, 为了不影响用户体验,规定必须使用静默更新. Google Play不能使用静默更新,必须弹框告知用户App有更新.中国 ...

  5. [RN] React Native 实现 类似京东 的 沉浸式状态栏和搜索栏

    React Native 实现 类似京东 的 沉浸式状态栏和搜索栏 其原理其实就是在要 隐藏 部分的那个View 前面加入 StatusBar 代码! 代码如下: <StatusBaranima ...

  6. [RN] React Native 自定义 底部 弹出 选择框 实现

    React Native 自定义 底部选择框 实现 效果如图所示: 实现方法: 一.组件封装 CustomAlertDialog.js import React, {Component} from ' ...

  7. [RN] React Native 错误 Module does not exist in the module map

    React Native 错误 Module does not exist in the module map 代码如下: import Login from 'login' import Index ...

  8. [RN] React Native 调试技巧

    React Native 调试技巧 一. 安卓模拟器调出Dev Setting 命令 adb shell input keyevent 82 二.图片不出来时,先运行此命令,再重新 run react ...

  9. [RN] React Native 自定义导航栏随滚动渐变

    React Native 自定义导航栏随滚动渐变 实现效果预览: 代码实现: 1.定义导航栏 NavPage.js import React, {Component} from 'react'; im ...

最新文章

  1. 扎克伯格亲自做了26张PPT,员工效率提10倍,已被疯狂传阅
  2. Python操作dict时避免出现KeyError的几种方法
  3. 这个开源项目帮你将Linux命令行一网打尽!
  4. gin redis 链接不上_php + redis 高并发商品秒杀 完整业务模拟流程 实现方案
  5. python批处理栅格转点_python获取栅格点和面值的实现
  6. 第四节 CSS继承性和层叠性
  7. 现代软件工程 第五章 【团队和流程】练习与讨论
  8. 互联网晚报 | 9月2日 星期四 | 小米汽车有限公司正式成立;唯品富邦消费金融获批开业;恒大举行保交楼军令状签署大会...
  9. iOS 开发----Xcode 因为证书问题经常报的那些错
  10. java实现蛇蛇大作战_蛇蛇大作战3D旋涡版
  11. 使用 docker+tmux 加强容器调度
  12. python-可变循环
  13. 2016蓝桥杯C++A:消除尾一(二进制运算)
  14. python nlpir_中文分词工具--NLPIR/ICTCLAS的Python版本使用
  15. 支持新老版帝国CMS采集入库教程
  16. OAuth2 oauth_client_details表字段的详细说明
  17. 不一样的国外广告联盟与cpa赚钱!
  18. 关于iPhone5耳机一个响解决办法(部分原因)
  19. 数据分析常用名称、统计分析模型 SUS、CLV、VOC、认知失调、 体验度量
  20. 这样做团建,还担心员工离职吗?

热门文章

  1. 2021年中国互联网企业100强(附名单)
  2. 计算机操作系统之设备管理思维导图
  3. linux下没有yum命令,linux下配置yum的三种方法与yum命令详解
  4. 访问网页出现503服务器,503错误,详细教您网页出现503错误怎么解决
  5. c语言错误不允许使用不完整的类型,C语言不允许使用不完整的类型报错是什么意思啊...
  6. jsp:session的跟踪方式
  7. Adobe Photoshop CS5永久序列号
  8. 纽约州推出“被遗忘权”提案 用户或能要求将个人隐私信息从搜索结果中移
  9. PNAS:整合抑郁症的分子、细胞和皮层神经影像特征
  10. WinEdt教程 表格,图像,公式,段落