NSScrollView 搞乱了 NSGradient (损坏)
我制作了一个自定义框,它是 NSBox 的子类。我重写 drawRect:
方法并在其中绘制渐变,如下所示(假设我已经有 start
和 end
颜色):
-(void)drawRect:(NSRect)dirtyRect {
NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:start endingColor:end];
[gradient drawInRect:dirtyRect angle:270];
[gradient release];
}
现在这个框被添加为 NSCollectionView 原型视图的子视图。在视图的原始状态下,它看起来像这样:
将视图滚动到视线之外并再次返回后,它看起来像这样:
为什么我的渐变会这样损坏,我该如何修复它?谢谢!
I have a custom box that I've made that is a subclass of NSBox
. I override the drawRect:
method and draw a gradient in it like this (assuming I already have a start
& end
color):
-(void)drawRect:(NSRect)dirtyRect {
NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:start endingColor:end];
[gradient drawInRect:dirtyRect angle:270];
[gradient release];
}
Now this box is added as a subview of a prototype view for a NSCollectionView
. In the view's original state it looks like this:
And after scrolling the view out of sight and back in again, it looks like this:
Why is my gradient getting corrupted like that, and how can I fix it? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该
dirtyRect
参数不一定代表整个盒子。如果 Cocoa 决定只有原始帧的一个子帧需要(重新)绘制,则dirtyRect
仅表示该子帧。如果您为整个帧绘制了渐变,然后(重新)为子帧绘制了相同的渐变,则它们可能不匹配。尝试:
相反。
进一步注意:看起来您的渐变对象可以被缓存,而不是在
-drawRect:
内创建/释放。That
dirtyRect
argument doesn’t necessarily represent the entire box. If Cocoa decides that only a subframe of the original frame needs (re)drawing,dirtyRect
represents only that subframe. If you’ve drawn a gradient for the entire frame and then (re)draw the same gradient for a subframe, it's likely they won't match.Try:
instead.
One further note: it looks like your gradient object could be cached instead of being created/released inside
-drawRect:
.您的问题是您正在
dirtyFrame
中绘制,而不是框的整个矩形。我不知道这是否正确,但试试这个:Your problem is you're drawing in
dirtyFrame
, not the entire rectangle of the box. I have no idea if this is correct, but try this: