-
Notifications
You must be signed in to change notification settings - Fork 10
/
GestureSwiper.js
174 lines (146 loc) · 4.55 KB
/
GestureSwiper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import React, { PureComponent } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
ViewPropTypes,
TouchableHighlight,
Animated,
PanResponder,
} from 'react-native';
import PropTypes from 'prop-types';
export default class SimpleSwiper extends PureComponent {
static propTypes = {
/**
* 设置外部容器的样式。
*/
style: ViewPropTypes.style,
/**
* 设置外部容器的默认宽度。
*/
initialWidth: PropTypes.number,
/**
* 轮播的视图
*/
index: PropTypes.number,
/**
* 当前展现项目要变化时,触发该回调。回调参数为变化后的位置。
*/
onChange: PropTypes.func,
/**
* 轮播项目
*/
children: PropTypes.node.isRequired,
};
static defaultProps = {
initialWidth: 0,
};
constructor(props) {
super(props);
this.state = {
layoutWidth: props.initialWidth,
positionAnimated: new Animated.Value(- props.index * props.initialWidth)
};
console.log('positionAnimated',- props.index * props.initialWidth)
}
componentWillMount() {
// 获取 positionAnimated 的动态值
this.state.positionAnimated.addListener(({ value }) => {
console.log('position',value)
this.position = value;
});
}
componentDidMount() {
}
componentDidUpdate(){
this.scrollTo(this.props.index)
}
onPanResponderEnd = () => {
// 超过 50% 的距离,触发左滑、右滑
const index = Math.round(-this.position / this.state.layoutWidth)
const safeIndex = this.getSafeIndex(index);
this.props.onChange(safeIndex)
};
responder = PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => true,
onMoveShouldSetPanResponder: (evt, gestureState) => true,
onShouldBlockNativeResponder: (evt, gestureState) => {
// 返回一个布尔值,决定当前组件是否应该阻止原生组件成为JS响应者
// 默认返回true。目前暂时只支持android。
return true;
},
onPanResponderTerminationRequest: (evt, gestureState) => true,
onPanResponderGrant: (evt, gestureState) => {
// 用户手指触碰屏幕,停止动画
this.state.positionAnimated.stopAnimation();
// 记录手势响应时的位置
this.startPosition = this.position;
},
onPanResponderMove: (evt, { dx }) => {
// 要变化的位置 = 手势响应时的位置 + 移动的距离
const position = this.startPosition + dx
this.state.positionAnimated.setValue(position)
},
onPanResponderRelease: this.onPanResponderEnd,
onPanResponderTerminate: this.onPanResponderEnd,
});
scrollTo = ( toIndex ) => {
Animated.spring(this.state.positionAnimated, {
toValue: - toIndex * this.state.layoutWidth,
friction: 12,
tension: 50,
}).start()
}
getSafeIndex = (index) => {
const max = this.props.children.length - 1;
const min = 0;
return Math.min(max, Math.max(index, min))
}
handleLayout = ({nativeEvent}) => {
this.setState({
layoutWidth: nativeEvent.layout.width,
})
this.state.positionAnimated.setValue( - this.props.index * nativeEvent.layout.width)
}
render() {
const {children, style, index} = this.props;
const {layoutWidth} = this.state;
const items = children.map((item, index) => (
<Animated.View
style={{
width: layoutWidth,
transform: [{
translateX: this.state.positionAnimated,
}],
}}
key={index}
>
{item}
</Animated.View>
));
return (
<View
style={[styles.container, style]}
onLayout={this.handleLayout}
{...this.responder.panHandlers}
>
{items}
</View>
)
}
}
const styles = StyleSheet.create({
container: {
overflow:'hidden',
flexDirection: 'row',
},
button: {
position: 'absolute',
top: 120,
width: 50,
height: 20,
borderWidth: 1,
borderColor: '#ccc',
}
})