Skip to content

Commit a6ddfa8

Browse files
committed
fix(laignedflowlayout): ios support for itemOverlap
1 parent 8ad6790 commit a6ddfa8

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

src/alignedflowlayout/index.ios.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CollectionView } from '@nativescript-community/ui-collectionview';
33

44
interface IOSCollectionView extends CollectionView {
55
clearCachedSize(...indexes: number[]);
6+
layoutAttributesForElementsInRect(attributesArray: NSArray<UICollectionViewLayoutAttributes>, rect: CGRect);
67
}
78

89
@NativeClass
@@ -29,20 +30,7 @@ class AlignedCollectionViewFlowLayoutImpl extends AlignedCollectionViewFlowLayou
2930
layoutAttributesForElementsInRect(rect: CGRect) {
3031
const attributesArray = super.layoutAttributesForElementsInRect(rect);
3132
const owner = this._owner?.get();
32-
if (owner?.itemOverlap) {
33-
const itemOverlap = owner.itemOverlap;
34-
for (let index = 0; index < attributesArray.count; index++) {
35-
const attributes = attributesArray.objectAtIndex(index);
36-
if (attributes.representedElementCategory === UICollectionElementCategory.Cell) {
37-
const xPosition =
38-
attributes.center.x + Utils.layout.toDeviceIndependentPixels(Length.toDevicePixels(itemOverlap[1], 0) + Length.toDevicePixels(itemOverlap[2], 0)) * attributes.indexPath.row;
39-
const yPosition =
40-
attributes.center.y + Utils.layout.toDeviceIndependentPixels(Length.toDevicePixels(itemOverlap[0], 0) + Length.toDevicePixels(itemOverlap[2], 0)) * attributes.indexPath.row;
41-
attributes.center = CGPointMake(xPosition, yPosition);
42-
}
43-
}
44-
}
45-
33+
owner?.layoutAttributesForElementsInRect(attributesArray, rect);
4634
return attributesArray;
4735
}
4836
shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) {
@@ -56,8 +44,8 @@ class AlignedCollectionViewFlowLayoutImpl extends AlignedCollectionViewFlowLayou
5644

5745
export default function install() {
5846
CollectionView.registerLayoutStyle('align', {
59-
createLayout: (collectionview: CollectionView) => {
60-
const layout = AlignedCollectionViewFlowLayoutImpl.new();
47+
createLayout: (collectionview: IOSCollectionView) => {
48+
const layout = AlignedCollectionViewFlowLayoutImpl.initWithOwner(collectionview);
6149
switch (collectionview['layoutHorizontalAlignment']) {
6250
case 'left':
6351
layout.horizontalAlignment = HorizontalAlignment.Left;

src/collectionview/index.ios.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,25 @@ export class CollectionView extends CollectionViewBase {
492492
indexes.forEach((index) => sizes.replaceObjectAtIndexWithObject(index, NSValue.valueWithCGSize(CGSizeZero)));
493493
}
494494
}
495+
public layoutAttributesForElementsInRect(attributesArray: NSArray<UICollectionViewLayoutAttributes>, rect: CGRect) {
496+
if (this.itemOverlap) {
497+
let currentDeltaX = 0;
498+
let currentDeltaY = 0;
499+
for (let index = 0; index < attributesArray.count; index++) {
500+
const attributes = attributesArray.objectAtIndex(index);
501+
if (attributes.representedElementCategory === UICollectionElementCategory.Cell) {
502+
const row = attributes.indexPath.row;
503+
if (this.itemOverlap) {
504+
attributes.zIndex = row;
505+
}
506+
const itemOverlap = this.itemOverlap(this.getItemAtIndex(row), row);
507+
currentDeltaX += Utils.layout.toDeviceIndependentPixels(Length.toDevicePixels(itemOverlap[1], 0) + Length.toDevicePixels(itemOverlap[3], 0));
508+
currentDeltaY += Utils.layout.toDeviceIndependentPixels(Length.toDevicePixels(itemOverlap[0], 0) + Length.toDevicePixels(itemOverlap[2], 0));
509+
attributes.center = CGPointMake(attributes.center.x + currentDeltaX, attributes.center.y + currentDeltaY);
510+
}
511+
}
512+
}
513+
}
495514

496515
public onSourceCollectionChanged(event: ChangedData<any>) {
497516
const view = this.nativeViewProtected;
@@ -1237,22 +1256,7 @@ class UICollectionViewFlowLayoutImpl extends UICollectionViewFlowLayout {
12371256
layoutAttributesForElementsInRect(rect: CGRect) {
12381257
const attributesArray = super.layoutAttributesForElementsInRect(rect);
12391258
const owner = this._owner?.get();
1240-
if (owner?.itemOverlap) {
1241-
for (let index = 0; index < attributesArray.count; index++) {
1242-
const attributes = attributesArray.objectAtIndex(index);
1243-
if (attributes.representedElementCategory === UICollectionElementCategory.Cell) {
1244-
const row = attributes.indexPath.row;
1245-
if (owner.itemOverlap) {
1246-
attributes.zIndex = row;
1247-
}
1248-
const itemOverlap = owner.itemOverlap(owner.getItemAtIndex(row), row);
1249-
const xPosition = attributes.center.x + Utils.layout.toDeviceIndependentPixels(Length.toDevicePixels(itemOverlap[1], 0) + Length.toDevicePixels(itemOverlap[2], 0)) * row;
1250-
const yPosition = attributes.center.y + Utils.layout.toDeviceIndependentPixels(Length.toDevicePixels(itemOverlap[0], 0) + Length.toDevicePixels(itemOverlap[2], 0)) * row;
1251-
attributes.center = CGPointMake(xPosition, yPosition);
1252-
}
1253-
}
1254-
}
1255-
1259+
owner?.layoutAttributesForElementsInRect(attributesArray, rect);
12561260
return attributesArray;
12571261
}
12581262
shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) {

0 commit comments

Comments
 (0)