Skip to content

Commit a08789e

Browse files
committed
fix(ios): startDragging pointer argument to fix drag translate
1 parent 40320e7 commit a08789e

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/collectionview-common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,9 @@ export abstract class CollectionViewBase extends View implements CollectionViewD
457457
}
458458
}
459459
abstract getViewForItemAtIndex(index: number): View;
460+
abstract startDragging(index: number);
460461
draggingView: View;
461462
_callItemReorderedEvent(oldPosition, newPosition, item) {
462-
console.log('_callItemReorderedEvent', this.draggingView);
463463
const args = {
464464
eventName: CollectionViewBase.itemReorderedEvent,
465465
object: this,
@@ -477,6 +477,7 @@ export abstract class CollectionViewBase extends View implements CollectionViewD
477477
const item = this.getItemAtIndex(oldPosition);
478478
ownerSource.splice(oldPosition, 1);
479479
ownerSource.splice(newPosition, 0, item);
480+
console.log('_reorderItemInSource', item, oldPosition, newPosition, this.items);
480481

481482
this.resumeUpdates(false);
482483
if (callEvents) {
@@ -491,7 +492,6 @@ export abstract class CollectionViewBase extends View implements CollectionViewD
491492
}
492493
const item = this.getItemAtIndex(index);
493494
const view = this.draggingView = this.getViewForItemAtIndex(index);
494-
console.log('shouldMoveItemAtIndex', this.draggingView);
495495
let args = {
496496
returnValue: true,
497497
eventName: CollectionViewBase.itemReorderStartingEvent,

src/collectionview.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { EventData, View } from '@nativescript/core';
2+
import { Pointer } from '@nativescript/core/ui/gestures';
23
import { CollectionViewBase } from './collectionview-common';
34
export * from './collectionview-common';
45

@@ -8,6 +9,10 @@ export class CollectionView extends CollectionViewBase {
89
public refresh();
910
public scrollToIndex(index: number, animated: boolean);
1011
public getViewForItemAtIndex(index: number): View;
12+
// on iOS a view is dragged from its center by default
13+
// if you use a drag "handle" just pass the touch event main pointer
14+
// to delta the dragging to be good
15+
startDragging(index: number, pointer?: Pointer);
1116
}
1217

1318
export interface CollectionViewItemEventData extends EventData {

src/collectionview.ios.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
paddingTopProperty,
1919
profile,
2020
} from '@nativescript/core';
21+
import { Pointer } from '@nativescript/core/ui/gestures';
2122
import { layout } from '@nativescript/core/utils/utils';
2223
import { CollectionViewItemDisplayEventData, CollectionViewItemEventData, Orientation, reorderLongPressEnabledProperty, reorderingEnabledProperty, reverseLayoutProperty } from './collectionview';
2324
import { CLog, CLogTypes, CollectionViewBase, ListViewViewTypes, isBounceEnabledProperty, isScrollEnabledProperty, itemTemplatesProperty, orientationProperty } from './collectionview-common';
@@ -203,9 +204,19 @@ export class CollectionView extends CollectionViewBase {
203204
}
204205
manualDragging = false;
205206
scrollEnabledBeforeDragging = true;
206-
public startDragging(index: number) {
207+
draggingStartDelta: [number, number];
208+
public startDragging(index: number, pointer?: Pointer) {
207209
if (this.reorderEnabled && this.nativeViewProtected) {
208210
this.manualDragging = true;
211+
this.draggingStartDelta = null;
212+
if (pointer) {
213+
const view = this.getViewForItemAtIndex(index);
214+
if (view) {
215+
const size = view.nativeViewProtected.bounds.size;
216+
const point = (pointer.ios as UITouch).locationInView(view.nativeViewProtected);
217+
this.draggingStartDelta = [point.x - size.width/2, point.y - size.height/2];
218+
}
219+
}
209220
this.nativeViewProtected.beginInteractiveMovementForItemAtIndexPath(NSIndexPath.indexPathForRowInSection(index, 0));
210221
this.scrollEnabledBeforeDragging = this.isScrollEnabled;
211222
this.nativeViewProtected.scrollEnabled = false;
@@ -219,7 +230,14 @@ export class CollectionView extends CollectionViewBase {
219230
const pointer = event.getActivePointers()[0];
220231
switch(event.action) {
221232
case 'move':
222-
collectionView.updateInteractiveMovementTargetPosition((pointer as any).location);
233+
let x = pointer.getX();
234+
let y = pointer.getY();
235+
if (this.draggingStartDelta) {
236+
console.log('updateInteractiveMovementTargetPosition', x, y, this.draggingStartDelta );
237+
x -= this.draggingStartDelta[0];
238+
y -= this.draggingStartDelta[1];
239+
}
240+
collectionView.updateInteractiveMovementTargetPosition(CGPointMake(x, y));
223241
break;
224242
case 'up':
225243
this.manualDragging = false;

0 commit comments

Comments
 (0)