1 Star 0 Fork 0

Stephen/react-native-head-tab-view

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ScrollHeader.js 2.91 KB
一键复制 编辑 原始数据 按行查看 历史
Stephen 提交于 2021-02-25 15:10 . 上传代码
import React from 'react';
import {
Animated,
PanResponder
} from 'react-native';
import PropTypes from 'prop-types';
export default class ScrollHeader extends React.PureComponent {
static propTypes = {
headerTrans: PropTypes.any.isRequired, //
onPanResponderGrant: PropTypes.func
}
constructor(props) {
super(props)
this.canResponder = false
const { headerTrans, onPanResponderGrant, headerStartResponder, headerStartCaptureResponder } = props
this.mPanResponder = PanResponder.create({
onStartShouldSetPanResponderCapture: (evt, gestureState) => {
headerStartCaptureResponder && headerStartCaptureResponder()
headerTrans.stopAnimation()
return false
},
onMoveShouldSetPanResponder: this.panResponderOnMove,
onMoveShouldSetPanResponderCapture: (evt, gestureState) => {
return this.canResponder
},
onPanResponderGrant: (evt, gestureState) => {
onPanResponderGrant && onPanResponderGrant()
},
onPanResponderMove: (evt, gestureState) => {
const { dx, dy } = gestureState;
this.canResponder = false
headerTrans.setValue(dy)
},
onPanResponderTerminationRequest: (evt, gestureState) => {
this.panResponderRelease(evt, gestureState)
return true;
},
onPanResponderTerminate: this.panResponderRelease,
onPanResponderRelease: this.panResponderRelease,
onShouldBlockNativeResponder: (evt, gestureState) => {
return true;
},
});
}
panResponderOnMove = (evt, gestureState) => {
const { headerTrans, headerMoveResponder } = this.props
if (Math.abs(gestureState.dx) < 10 && Math.abs(gestureState.dy) < 10) {
headerMoveResponder && headerMoveResponder()
return false;
}
if (!this.canResponder) {
headerTrans.stopAnimation(() => {
this.canResponder = Math.abs(gestureState.dy) > Math.abs(gestureState.dx);
})
return false;
}
return true;
}
panResponderRelease = (evt, gestureState) => {
const { headerTrans, headerReleaseResponder } = this.props
headerReleaseResponder && headerReleaseResponder()
if (Math.abs(gestureState.dy) < 2 || gestureState.vy == 0) return;
Animated.decay(
headerTrans,
{
velocity: gestureState.vy,
deceleration: 0.998,
useNativeDriver: true,
},
).start();
}
render() {
return (
<Animated.View style={this.props.style} {...this.mPanResponder.panHandlers}>
{this.props.children}
</Animated.View>
)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/yaojinye/react-native-head-tab-view.git
git@gitee.com:yaojinye/react-native-head-tab-view.git
yaojinye
react-native-head-tab-view
react-native-head-tab-view
master

搜索帮助