@@ -7,21 +7,22 @@ import React, {
77import { StyleProp , StyleSheet , ViewStyle } from "react-native" ;
88import Animated , {
99 useAnimatedStyle ,
10- useAnimatedGestureHandler ,
1110 useSharedValue ,
1211 withSpring ,
1312 useDerivedValue ,
1413 useAnimatedReaction ,
1514 runOnJS ,
1615 interpolate ,
16+ WithSpringConfig ,
1717} from "react-native-reanimated" ;
1818import {
19- GestureEvent ,
20- PanGestureHandler ,
21- PanGestureHandlerEventPayload ,
19+ ComposedGesture ,
20+ Gesture ,
21+ GestureDetector ,
22+ GestureType ,
2223} from "react-native-gesture-handler" ;
2324
24- export const DEFAULT_ANIMATION_CONFIG : Animated . WithSpringConfig = {
25+ export const DEFAULT_ANIMATION_CONFIG : WithSpringConfig = {
2526 damping : 20 ,
2627 mass : 0.2 ,
2728 stiffness : 100 ,
@@ -52,9 +53,9 @@ type Props = {
5253 pageInterpolator ?: typeof defaultPageInterpolator ;
5354 minIndex ?: number ;
5455 maxIndex ?: number ;
55- simultaneousHandlers ?: React . Ref < unknown > | React . Ref < unknown > [ ] ;
56+ simultaneousGestures ?: ( ComposedGesture | GestureType ) [ ] ;
5657 gesturesDisabled ?: boolean ;
57- animationConfig ?: Partial < Animated . WithSpringConfig > ;
58+ animationConfig ?: Partial < WithSpringConfig > ;
5859} ;
5960
6061type ImperativeApiOptions = {
@@ -78,7 +79,7 @@ function InfinitePager(
7879 pageInterpolator = defaultPageInterpolator ,
7980 minIndex = - Infinity ,
8081 maxIndex = Infinity ,
81- simultaneousHandlers ,
82+ simultaneousGestures = [ ] ,
8283 gesturesDisabled,
8384 animationConfig = { } ,
8485 } : Props ,
@@ -158,48 +159,42 @@ function InfinitePager(
158159 [ ]
159160 ) ;
160161
161- const gestureHandler = useAnimatedGestureHandler <
162- GestureEvent < PanGestureHandlerEventPayload > ,
163- { startX : number }
164- > (
165- {
166- onStart : ( _ , ctx ) => {
167- ctx . startX = translateX . value ;
168- } ,
169- onActive : ( event , ctx ) => {
170- const rawVal = ctx . startX + event . translationX ;
171- const page = - rawVal / pageWidth . value ;
172- if ( page >= minIndex && page <= maxIndex ) {
173- translateX . value = rawVal ;
174- }
175- } ,
176- onEnd : ( evt ) => {
177- const isFling = Math . abs ( evt . velocityX ) > 500 ;
178- let velocityModifier = isFling ? pageWidth . value / 2 : 0 ;
179- if ( evt . velocityX < 0 ) velocityModifier *= - 1 ;
180- let page =
181- - 1 *
182- Math . round ( ( translateX . value + velocityModifier ) / pageWidth . value ) ;
183- if ( page < minIndex ) page = minIndex ;
184- if ( page > maxIndex ) page = maxIndex ;
162+ const startX = useSharedValue ( 0 ) ;
163+
164+ const panGesture = Gesture . Pan ( )
165+ . onBegin ( ( ) => {
166+ startX . value = translateX . value ;
167+ } )
168+ . onUpdate ( ( evt ) => {
169+ const rawVal = startX . value + evt . translationX ;
170+ const page = - rawVal / pageWidth . value ;
171+ if ( page >= minIndex && page <= maxIndex ) {
172+ translateX . value = rawVal ;
173+ }
174+ } )
175+ . onEnd ( ( evt ) => {
176+ const isFling = Math . abs ( evt . velocityX ) > 500 ;
177+ let velocityModifier = isFling ? pageWidth . value / 2 : 0 ;
178+ if ( evt . velocityX < 0 ) velocityModifier *= - 1 ;
179+ let page =
180+ - 1 *
181+ Math . round ( ( translateX . value + velocityModifier ) / pageWidth . value ) ;
182+ if ( page < minIndex ) page = minIndex ;
183+ if ( page > maxIndex ) page = maxIndex ;
185184
186- const animCfg = Object . assign (
187- { } ,
188- DEFAULT_ANIMATION_CONFIG ,
189- animCfgRef . current
190- ) ;
185+ const animCfg = Object . assign (
186+ { } ,
187+ DEFAULT_ANIMATION_CONFIG ,
188+ animCfgRef . current
189+ ) ;
191190
192- translateX . value = withSpring ( - page * pageWidth . value , animCfg ) ;
193- } ,
194- } ,
195- [ minIndex , maxIndex ]
196- ) ;
191+ translateX . value = withSpring ( - page * pageWidth . value , animCfg ) ;
192+ } )
193+ . enabled ( ! gesturesDisabled ) ;
197194
198195 return (
199- < PanGestureHandler
200- enabled = { ! gesturesDisabled }
201- onGestureEvent = { gestureHandler }
202- simultaneousHandlers = { simultaneousHandlers }
196+ < GestureDetector
197+ gesture = { Gesture . Simultaneous ( panGesture , ...simultaneousGestures ) }
203198 >
204199 < Animated . View
205200 style = { style }
@@ -222,7 +217,7 @@ function InfinitePager(
222217 ) ;
223218 } ) }
224219 </ Animated . View >
225- </ PanGestureHandler >
220+ </ GestureDetector >
226221 ) ;
227222}
228223
@@ -284,11 +279,12 @@ const PageWrapper = React.memo(
284279 const animStyle = useAnimatedStyle ( ( ) => {
285280 // Short circuit page interpolation to prevent buggy initial values due to possible race condition:
286281 // https://github.com/software-mansion/react-native-reanimated/issues/2571
287- if ( ! pageWidth . value ) return { } ;
282+ const isInactivePageBeforeInit = index !== 0 && ! pageWidth . value ;
283+ const _pageWidth = isInactivePageBeforeInit ? focusAnim : pageWidth ;
288284 return pageInterpolatorRef . current ( {
289285 focusAnim,
290286 pageAnim,
291- pageWidth,
287+ pageWidth : _pageWidth ,
292288 index,
293289 } ) ;
294290 } , [ pageWidth , pageAnim , index , translation ] ) ;
0 commit comments