gpt4 book ai didi

objective-c - NSCollectionView的visibleRect属性

转载 作者:行者123 更新时间:2023-12-03 17:18:34 26 4
gpt4 key购买 nike

NSCollectionView 显示多个项目。我有一个类似网格的布局。

我需要确定当前可见的项目。

根据documentation有一个方法

- (NSArray<NSCollectionViewItem *> *)visibleItems;

带有描述

The items returned by this method represent the ones that are active and currently being managed by the collection view. This array may contain items that are outside of the collection view’s actual visible rectangle. For example, it may contain items that were recently visible but have since been scrolled out of view. To test whether an item is actually visible, check to see if its frame rectangle intersects the visibleRect of the collection view.

所以我需要根据其frame坐标和collectionView的visibleRect手动计算哪些项目是可见的。当然没问题。

但是,令我困扰的是 visibleRect 返回的是哪个坐标。

通常,它与 documentation here 一致如果我有的话可以说

NSImage *img = [[NSImage alloc] initWithContentsOfFile:...];

[imageView setFrame:NSMakeRect(0., -100., 300., 400.)];
[imageView setImage:img];
NSLog(..., [imageView visibleRect]);
NSLog(..., [imageView frame]);

那么可见矩形将具有预期的效果

x=0
y=100
width = 300
height = 300

这与文档完全一致。

但是,在 NSCollectionView 的情况下,它的行为有所不同。如果我创建了项目并且显示了第一个项目,则将显示 visibleRect

x = 0
y = 0

现在,如果我向下滚动一点,输出将是

x = 0
y = some number > 0

但是,我希望它应该返回不同的 y 值。因为

A view's visible rectangle reflects the portion of the contents that are actually displayed, in terms of the view's bounds coordinate system

假设项目之间没有间距并且项目相同

x = 0
y = (numItems - 1) * itemHeight

enter image description here

这种行为当然不是问题。我可以使用这些值根据返回值进行计算。我想了解一下原因。

我错过了什么?

附注本质上, NSCollectionView visibleRect 及其项目的 frame 的行为就好像坐标系的原点位于左上角且 y 向下增加,x 向右增加。

最佳答案

NSCollectionView 使用翻转的坐标系。请参阅flipped NSViewFlipped Coordinate Systems 的属性.

关于objective-c - NSCollectionView的visibleRect属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55170505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com