gpt4 book ai didi

objective-c - 动画期间半透明 NSTableView 背景绘制错误

转载 作者:行者123 更新时间:2023-12-03 17:51:31 25 4
gpt4 key购买 nike

我有一个基于 View 的 NSTableView ,具有半透明背景。除了行动画期间之外,这工作得很好。如果我对 NSTableView 背景使用完全不透明的颜色,那么就没有问题。这是显示问题的 gif:

http://gfycat.com/ChillyVigorousChamois

正如 gif 所示,在动画过程中,背景闪烁着比应有的深红色。如果仔细观察,实际上有一部分背景绘制正确,它向下滑动以容纳新添加的行。

NSTableView 后面的 NSScrollView 是纯白色,如下代码设置:

-(void) awakeFromNib {
self.drawsBackground = YES;
self.backgroundColor = [NSColor whiteColor];
}

NSTableView 具有 50% 透明的红色背景,如以下代码所示:

-(BOOL) isOpaque {
return NO;
}

- (void)drawBackgroundInClipRect:(NSRect)clipRect {
[[[NSColor redColor] colorWithAlphaComponent:0.5] setFill];
NSRectFillUsingOperation(clipRect, NSCompositeSourceOver);
}

使用以下代码对行进行动画处理:

[_tableView insertRowsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:insertRange]
withAnimation:NSTableViewAnimationSlideLeft];

我目前最好的猜测是,变暗的发生是因为在动画过程中背景被重复地绘制在其自身之上。过去,我通过将父 View 添加到 NSAnimation 来修复类似的错误,这迫使它与动画 subview 一起重复重绘。在这种情况下,我不知道如何做同样的事情。

有什么想法可以解决这个问题吗?

最佳答案

这并不能真正解决问题,但这里有一个解决方法。诀窍是将整个 NSScrollView 放入自定义 NSView 中,并在自定义 NSView 中绘制背景。

使NSTableView透明:

@implementation SJTableView
- (BOOL)isOpaque {
return NO;
}

-(void) drawBackgroundInClipRect:(NSRect)clipRect {
NSRectFillUsingOperation(clipRect, NSCompositeClear);
}
@end

使NSScrollView透明:

_scrollView.drawsBackground = NO;

在自定义 View 中绘制背景:

@implementation SJView
-(BOOL) isOpaque {
return (_backgroundColor.alphaComponent >= 1.0);
}

-(void) drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];

if(_backgroundColor){
[_backgroundColor setFill];
NSRectFillUsingOperation(dirtyRect, NSCompositeSourceOver);
}
}
@end

关于objective-c - 动画期间半透明 NSTableView 背景绘制错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25377717/

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