@@ -246,6 +246,21 @@ class Carousel extends Component<CarouselProps, CarouselState> {
246
246
}
247
247
} ;
248
248
249
+ getInitialContentOffset = ( snapToOffsets : number [ ] | undefined ) => {
250
+ const { horizontal, initialPage} = this . props ;
251
+ const { initialOffset} = this . state ;
252
+ let contentOffset = initialOffset ;
253
+ if ( snapToOffsets && initialPage !== undefined ) {
254
+ const offset = snapToOffsets [ initialPage ] ;
255
+ contentOffset = {
256
+ x : horizontal ? offset : 0 ,
257
+ y : horizontal ? 0 : offset
258
+ } ;
259
+ }
260
+
261
+ return contentOffset ;
262
+ }
263
+
249
264
shouldUsePageWidth ( ) {
250
265
const { loop, pageWidth} = this . props ;
251
266
return ! loop && pageWidth ;
@@ -477,13 +492,13 @@ class Carousel extends Component<CarouselProps, CarouselState> {
477
492
478
493
renderCarousel ( ) {
479
494
const { containerStyle, animated, horizontal, animatedScrollOffset, ...others } = this . props ;
480
- const { initialOffset} = this . state ;
481
495
const scrollContainerStyle = this . shouldUsePageWidth ( )
482
496
? { paddingRight : this . getItemSpacings ( this . props ) }
483
497
: undefined ;
484
498
const snapToOffsets = this . getSnapToOffsets ( ) ;
485
499
const marginBottom = Math . max ( 0 , this . getContainerPaddingVertical ( ) - 16 ) ;
486
500
const ScrollContainer = animatedScrollOffset ? Animated . ScrollView : ScrollView ;
501
+ const contentOffset = this . getInitialContentOffset ( snapToOffsets ) ;
487
502
return (
488
503
< View
489
504
animated = { animated }
@@ -502,8 +517,8 @@ class Carousel extends Component<CarouselProps, CarouselState> {
502
517
horizontal = { horizontal }
503
518
pagingEnabled = { this . shouldEnablePagination ( ) }
504
519
snapToOffsets = { snapToOffsets }
505
- contentOffset = { initialOffset } // iOS only
506
- onContentSizeChange = { this . onContentSizeChange }
520
+ contentOffset = { contentOffset }
521
+ // onContentSizeChange={this.onContentSizeChange}
507
522
onMomentumScrollEnd = { this . onMomentumScrollEnd }
508
523
>
509
524
{ this . renderChildren ( ) }
0 commit comments