- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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
这种行为当然不是问题。我可以使用这些值根据返回值进行计算。我想了解一下原因。
我错过了什么?
附注本质上, NSCollectionView
visibleRect
及其项目的 frame
的行为就好像坐标系的原点位于左上角且 y
向下增加,x
向右增加。
最佳答案
NSCollectionView
使用翻转的坐标系。请参阅flipped NSView
和 Flipped Coordinate Systems 的属性.
关于objective-c - NSCollectionView的visibleRect属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55170505/
我希望在某个 NSView 时收到通知的 - (NSRect)visibleRect更改是因为我想根据可见矩形做一些花哨的 subview 布局。坦率地说,现在我很困惑; -visibleRect不发
我是一名优秀的程序员,十分优秀!