@@ -9,10 +9,12 @@ import PanningProvider from './panningProvider';
9
9
const DEFAULT_SPEED = 20 ;
10
10
const DEFAULT_BOUNCINESS = 6 ;
11
11
const DEFAULT_DISMISS_ANIMATION_DURATION = 280 ;
12
+ const MAXIMUM_DRAGS_AFTER_SWIPE = 2 ;
12
13
13
14
/**
14
15
* @description : PanDismissibleView component created to making listening to swipe and drag events easier,
15
- * Has to be used as a child of a PanningProvider that also has a PanListenerView
16
+ * @notes : Has to be used as a child of a PanningProvider that also has a PanListenerView.
17
+ * The PanListenerView is the one that sends the drag\swipe events.
16
18
*/
17
19
class PanDismissibleView extends PureComponent {
18
20
static displayName = 'PanDismissibleView' ;
@@ -60,8 +62,9 @@ class PanDismissibleView extends PureComponent {
60
62
this . state = {
61
63
animTranslateX : new Animated . Value ( 0 ) ,
62
64
animTranslateY : new Animated . Value ( 0 ) ,
63
- isAnimating : false ,
65
+ isAnimating : false
64
66
} ;
67
+ shouldDismissAfterReset = false ;
65
68
this . ref = React . createRef ( ) ;
66
69
}
67
70
@@ -110,22 +113,31 @@ class PanDismissibleView extends PureComponent {
110
113
}
111
114
} ;
112
115
113
- initPositions = ( ) => {
116
+ initPositions = ( extraDataForSetState , runAfterSetState ) => {
114
117
this . setNativeProps ( 0 , 0 ) ;
115
118
this . setState ( {
116
119
animTranslateX : new Animated . Value ( 0 ) ,
117
120
animTranslateY : new Animated . Value ( 0 ) ,
118
- } ) ;
121
+ ...extraDataForSetState
122
+ } , runAfterSetState ) ;
119
123
} ;
120
124
121
125
onPanStart = ( ) => {
122
126
this . swipe = { } ;
127
+ this . counter = 0 ;
123
128
} ;
124
129
125
130
onDrag = deltas => {
126
131
const left = deltas . x ? Math . round ( deltas . x ) : 0 ;
127
132
const top = deltas . y ? Math . round ( deltas . y ) : 0 ;
128
133
this . setNativeProps ( left , top ) ;
134
+ if ( this . swipe . x || this . swipe . y ) {
135
+ if ( this . counter < MAXIMUM_DRAGS_AFTER_SWIPE ) {
136
+ this . counter += 1 ;
137
+ } else {
138
+ this . swipe = { } ;
139
+ }
140
+ }
129
141
} ;
130
142
131
143
setNativeProps = ( left , top ) => {
@@ -156,12 +168,12 @@ class PanDismissibleView extends PureComponent {
156
168
const { isRight, isDown} = this . getDismissAnimationDirection ( ) ;
157
169
this . _animateDismiss ( isRight , isDown ) ;
158
170
} else {
159
- this . animateToInitialPosition ( ) ;
171
+ this . resetPosition ( ) ;
160
172
}
161
173
}
162
174
} ;
163
175
164
- animateToInitialPosition = ( ) => {
176
+ resetPosition = ( ) => {
165
177
const { animTranslateX, animTranslateY} = this . state ;
166
178
const { speed, bounciness} = this . props . animationOptions ;
167
179
const toX = - this . left ;
@@ -188,13 +200,14 @@ class PanDismissibleView extends PureComponent {
188
200
}
189
201
190
202
this . setState ( { isAnimating : true } , ( ) => {
191
- Animated . parallel ( animations ) . start ( this . onInitAnimationFinished ) ;
203
+ Animated . parallel ( animations ) . start ( this . onResetPositionFinished ) ;
192
204
} ) ;
193
205
} ;
194
206
195
- onInitAnimationFinished = ( ) => {
196
- this . setState ( { isAnimating : false } ) ;
197
- this . initPositions ( ) ;
207
+ onResetPositionFinished = ( ) => {
208
+ const runAfterSetState = this . shouldDismissAfterReset ? this . animateDismiss : undefined ;
209
+ this . shouldDismissAfterReset = false ;
210
+ this . initPositions ( { isAnimating : false } , runAfterSetState ) ;
198
211
} ;
199
212
200
213
getDismissAnimationDirection = ( ) => {
@@ -232,14 +245,19 @@ class PanDismissibleView extends PureComponent {
232
245
// isDown === true --> animate to the bottom
233
246
// isDown === false --> animate to the top
234
247
animateDismiss = ( ) => {
235
- const { directions = [ ] } = this . props ;
236
- const hasUp = directions . includes ( PanningProvider . Directions . UP ) ;
237
- const hasRight = directions . includes ( PanningProvider . Directions . RIGHT ) ;
238
- const hasLeft = directions . includes ( PanningProvider . Directions . LEFT ) ;
239
- const hasDown = ! hasUp && ! hasLeft && ! hasRight ; // default
240
- const verticalDismiss = hasDown ? true : hasUp ? false : undefined ;
241
- const horizontalDismiss = hasRight ? true : hasLeft ? false : undefined ;
242
- this . _animateDismiss ( horizontalDismiss , verticalDismiss ) ;
248
+ const { isAnimating} = this . state ;
249
+ if ( isAnimating ) {
250
+ this . shouldDismissAfterReset = true ;
251
+ } else {
252
+ const { directions = [ ] } = this . props ;
253
+ const hasUp = directions . includes ( PanningProvider . Directions . UP ) ;
254
+ const hasRight = directions . includes ( PanningProvider . Directions . RIGHT ) ;
255
+ const hasLeft = directions . includes ( PanningProvider . Directions . LEFT ) ;
256
+ const hasDown = ! hasUp && ! hasLeft && ! hasRight ; // default
257
+ const verticalDismiss = hasDown ? true : hasUp ? false : undefined ;
258
+ const horizontalDismiss = hasRight ? true : hasLeft ? false : undefined ;
259
+ this . _animateDismiss ( horizontalDismiss , verticalDismiss ) ;
260
+ }
243
261
} ;
244
262
245
263
_animateDismiss = ( isRight , isDown ) => {
0 commit comments