@@ -63,6 +63,7 @@ class UICellView extends UIView {
63
63
64
64
const PFLAG_FORCE_LAYOUT = 1 ;
65
65
export class Pager extends PagerBase {
66
+
66
67
lastEvent : number = 0 ;
67
68
private mDisableSwipe : boolean = false ;
68
69
private mDisableAnimation : boolean = false ;
@@ -177,6 +178,13 @@ export class Pager extends PagerBase {
177
178
}
178
179
}
179
180
181
+ /**
182
+ *
183
+ * Get the selected index from the position in the CollectionView
184
+ *
185
+ * @param index The position in the collectionView
186
+ * @returns The selected Index ( i.e. the number in the slides as the user would view it).
187
+ */
180
188
getPosition ( index : number ) : number {
181
189
let position = index ;
182
190
if ( this . circularMode ) {
@@ -191,6 +199,39 @@ export class Pager extends PagerBase {
191
199
return position ;
192
200
}
193
201
202
+ /**
203
+ *
204
+ * Get the position in the CollectionView from the selected index
205
+ *
206
+ * @param index The position in the collectionView
207
+ * @returns The selected Index ( i.e. the number in the slides as the user would view it).
208
+ */
209
+ getIndex ( index : number ) : number {
210
+ let position = index ;
211
+ if ( this . circularMode ) {
212
+ if ( position === 0 ) {
213
+ position = 1 ;
214
+ } else if ( position === this . firstDummy ) {
215
+ position = 0 ;
216
+ } else {
217
+ position = position + 1 ;
218
+ }
219
+ }
220
+ return position ;
221
+ }
222
+
223
+ animateForFlip ( index : number ) : boolean {
224
+ console . log ( "AnimateForFlip" , index , this . firstDummy ) ;
225
+ if ( ! this . circularMode ) {
226
+ return true ;
227
+ }
228
+
229
+ if ( index !== this . firstDummy && index !== 0 ) {
230
+ return true ;
231
+ }
232
+ return false ;
233
+ }
234
+
194
235
get itemCount ( ) : number {
195
236
return this . _childrenCount ? this . _childrenCount + ( this . circularMode ? 2 : 0 ) : 0 ;
196
237
}
@@ -269,9 +310,9 @@ export class Pager extends PagerBase {
269
310
}
270
311
271
312
[ itemsProperty . setNative ] ( value : any ) {
272
- // if (this.indicatorView && value && value.length) {
273
- // this.indicatorView.numberOfPages = value.length;
274
- // }
313
+ if ( this . indicator && value && value . length ) {
314
+ this . indicator . setCount ( value . length ) ;
315
+ }
275
316
this . setObservableArrayInstance ( value ) ;
276
317
277
318
if ( ! value ) {
@@ -297,9 +338,9 @@ export class Pager extends PagerBase {
297
338
if ( ! this . nativeViewProtected ) {
298
339
return ;
299
340
}
300
- // if (this.indicatorView && this.mObservableArrayInstance && this.mObservableArrayInstance.length) {
301
- // this.indicatorView.numberOfPages = this.mObservableArrayInstance.length;
302
- // }
341
+ if ( this . indicator && this . mObservableArrayInstance && this . mObservableArrayInstance . length ) {
342
+ this . indicator . setCount ( this . mObservableArrayInstance . length ) ;
343
+ }
303
344
304
345
const collectionView = this . nativeViewProtected ;
305
346
if ( collectionView ) {
@@ -328,7 +369,9 @@ export class Pager extends PagerBase {
328
369
array . push ( NSIndexPath . indexPathForRowInSection ( args . index + i , 0 ) ) ;
329
370
}
330
371
collectionView . deleteItemsAtIndexPaths ( array ) ;
331
- } else {
372
+ }
373
+
374
+ if ( args . addedCount > 0 ) {
332
375
const addedArray = [ ] ;
333
376
for ( let i = 0 ; i < args . addedCount ; i ++ ) {
334
377
addedArray . push ( NSIndexPath . indexPathForRowInSection ( args . index + i , 0 ) ) ;
@@ -354,6 +397,7 @@ export class Pager extends PagerBase {
354
397
_onItemsChanged ( oldValue : any , newValue : any ) : void { }
355
398
356
399
scrollToIndexAnimated ( index : number , animate : boolean ) {
400
+ console . log ( 'scrollToIndexAnimated' , index , animate ) ;
357
401
if ( ! this . nativeViewProtected ) return ;
358
402
359
403
const contentSize = this . nativeViewProtected . contentSize ;
@@ -384,7 +428,7 @@ export class Pager extends PagerBase {
384
428
// Reference: https://stackoverflow.com/a/53798708/6015400
385
429
this . nativeViewProtected . setContentOffsetAnimated ( CGPointMake ( 1 , 0 ) , ! ! animate ) ;
386
430
this . nativeViewProtected . scrollToItemAtIndexPathAtScrollPositionAnimated (
387
- NSIndexPath . indexPathForItemInSection ( maxMinIndex , 0 ) ,
431
+ NSIndexPath . indexPathForItemInSection ( this . getIndex ( maxMinIndex ) , 0 ) ,
388
432
this . orientation === 'vertical' ? UICollectionViewScrollPosition . CenteredVertically : UICollectionViewScrollPosition . CenteredHorizontally ,
389
433
! ! animate
390
434
) ;
@@ -418,6 +462,10 @@ export class Pager extends PagerBase {
418
462
this . _updateScrollPosition ( ) ;
419
463
this . _initAutoPlay ( this . autoPlay ) ;
420
464
// });
465
+
466
+ if ( this . indicator ) {
467
+ this . indicator . setCount ( this . _childrenCount ) ;
468
+ }
421
469
}
422
470
423
471
_isDataDirty = false ;
@@ -750,6 +798,7 @@ class PagerCell extends UICollectionViewCell {
750
798
@ObjCClass ( UICollectionViewDelegate , UICollectionViewDelegateFlowLayout )
751
799
class UICollectionDelegateImpl extends NSObject implements UICollectionViewDelegate , UICollectionViewDelegateFlowLayout {
752
800
private _owner : WeakRef < Pager > ;
801
+ isScrolling = false ;
753
802
754
803
public static initWithOwner ( owner : WeakRef < Pager > ) : UICollectionDelegateImpl {
755
804
const delegate = UICollectionDelegateImpl . alloc ( ) . init ( ) as UICollectionDelegateImpl ;
@@ -839,8 +888,9 @@ class UICollectionDelegateImpl extends NSObject implements UICollectionViewDeleg
839
888
object : owner
840
889
} ) ;
841
890
}
891
+ console . log ( "Scroll ended" ) ;
842
892
}
843
-
893
+
844
894
public scrollViewDidScroll ( scrollView : UIScrollView ) : void {
845
895
const owner = this . _owner . get ( ) ;
846
896
if ( owner ) {
@@ -866,6 +916,12 @@ class UICollectionDelegateImpl extends NSObject implements UICollectionViewDeleg
866
916
if ( owner . selectedIndex !== index && ! Number . isNaN ( index ) ) {
867
917
// selectedIndexProperty.nativeValueChange(owner, index);
868
918
}
919
+ if ( owner . indicator && ! Number . isNaN ( index ) ) {
920
+ if ( owner . circularMode && ( index === 0 || index === owner . firstDummy ) ) {
921
+ } else {
922
+ owner . indicator . setSelection ( owner . getPosition ( index ) ) ;
923
+ }
924
+ }
869
925
owner . notify ( {
870
926
object : owner ,
871
927
eventName : Pager . scrollEvent ,
@@ -887,18 +943,48 @@ class UICollectionDelegateImpl extends NSObject implements UICollectionViewDeleg
887
943
// NSIndexPath.indexPathForRowInSection(Math.round(width),0), UICollectionViewScrollPosition.CenteredHorizontally, true
888
944
// );
889
945
890
- // if(owner.circularMode){
891
- // if(nextIndex === 0){
892
- // selectedIndexProperty.nativeValueChange(owner, owner._childrenCount - 3);
893
- // }else if(nextIndex === owner._childrenCount -1){
894
- // selectedIndexProperty.nativeValueChange(owner, 0);
895
- // }else {
896
- // selectedIndexProperty.nativeValueChange(owner, nextIndex - 1);
897
- // }
898
- // }else {
899
- // selectedIndexProperty.nativeValueChange(owner, nextIndex);
900
- // }
901
-
946
+ if ( owner . circularMode ) {
947
+ console . log ( 'CIRCULAR' , index ) ;
948
+ /* if (index === 0) {
949
+ selectedIndexProperty.nativeValueChange(owner, owner._childrenCount - 3);
950
+ } else if (index === owner._childrenCount - 1) {
951
+ selectedIndexProperty.nativeValueChange(owner, 0);
952
+ } else {
953
+ selectedIndexProperty.nativeValueChange(owner, index - 1);
954
+ }
955
+ */
956
+ if ( ! this . isScrolling ) {
957
+ this . isScrolling = true ;
958
+ const contentOffset = scrollView . contentOffset ;
959
+ const contentSize = scrollView . contentSize ;
960
+ const frameSize = scrollView . frame . size ;
961
+ if ( contentOffset . x < 0 ) {
962
+ scrollView . contentOffset = CGPointMake ( contentSize . width - frameSize . width * 2 , 0 ) ;
963
+ if ( owner . indicator ) {
964
+ owner . indicator . setSelection ( owner . lastIndex , false ) ;
965
+ }
966
+ } else if ( contentOffset . x + frameSize . width > contentSize . width ) {
967
+ scrollView . contentOffset = CGPointMake ( frameSize . width , 0 ) ;
968
+ owner . indicator . setSelection ( 0 , false ) ;
969
+ }
970
+ console . log ( 'Doing circular' , contentOffset . x , frameSize . width , contentSize . width ) ;
971
+
972
+ if ( contentOffset . x === 0 ) {
973
+ console . log ( "Moving point 1" ) ;
974
+ scrollView . contentOffset = CGPointMake ( contentSize . width - frameSize . width * 2 , 0 ) ;
975
+ if ( owner . indicator ) {
976
+ console . log ( "Moving point 2" ) ;
977
+ owner . indicator . setSelection ( owner . lastIndex , false ) ;
978
+ }
979
+ } else if ( contentOffset . x + frameSize . width === contentSize . width ) {
980
+ console . log ( "Moving point 3" ) ;
981
+ scrollView . contentOffset = CGPointMake ( frameSize . width , 0 ) ;
982
+ console . log ( "Moving point 4" ) ;
983
+ owner . indicator . setSelection ( 0 , false ) ;
984
+ }
985
+ this . isScrolling = false ;
986
+ }
987
+ }
902
988
/* if (!Number.isNaN(width)) {
903
989
let page = Math.ceil(width);
904
990
const doScroll = () => {
@@ -1180,7 +1266,7 @@ class UICollectionViewFlowLinearLayoutImpl extends UICollectionViewFlowLayout {
1180
1266
const flickedPages = Math . abs ( Math . round ( flickVelocity ) ) <= 1 ? 0 : Math . round ( flickVelocity ) ;
1181
1267
1182
1268
const newPageIndex = currentPage + flickedPages ;
1183
- selectedIndexProperty . nativeValueChange ( owner , Math . min ( Math . max ( newPageIndex , 0 ) , owner . _childrenCount - 1 ) ) ;
1269
+ selectedIndexProperty . nativeValueChange ( owner , owner . getPosition ( Math . min ( Math . max ( newPageIndex , 0 ) , owner . _childrenCount - 1 ) ) ) ;
1184
1270
// Calculate newHorizontalOffset.
1185
1271
const newHorizontalOffset = newPageIndex * pageWidth - this . collectionView . contentInset . left ;
1186
1272
@@ -1203,7 +1289,7 @@ class UICollectionViewFlowLinearLayoutImpl extends UICollectionViewFlowLayout {
1203
1289
const flickedPages = Math . abs ( Math . round ( flickVelocity ) ) <= 1 ? 0 : Math . round ( flickVelocity ) ;
1204
1290
1205
1291
const newPageIndex = currentPage + flickedPages ;
1206
- selectedIndexProperty . nativeValueChange ( owner , Math . min ( Math . max ( newPageIndex , 0 ) , owner . _childrenCount - 1 ) ) ;
1292
+ selectedIndexProperty . nativeValueChange ( owner , owner . getPosition ( Math . min ( Math . max ( newPageIndex , 0 ) , owner . _childrenCount - 1 ) ) ) ;
1207
1293
const newVerticalOffset = newPageIndex * pageHeight - this . collectionView . contentInset . top ;
1208
1294
1209
1295
return CGPointMake ( proposedContentOffset . x , newVerticalOffset ) ;
0 commit comments