Cocoa 如何在NSScrollView中隐藏documentView?
如何在Cocoa 如何在NSScrollView中隐藏documentView?,cocoa,Cocoa,如何在NSScrollView中隐藏documentView 效果看起来像iBook作者: 您需要在文档视图中插入内容,以便为阴影显示留出空间,然后将视图分层并在其上设置阴影。例如: view.wantsLayer = YES; NSShadow *shadow = [NSShadow new]; shadow.shadowColor = [NSColor blackColor] shadow.shadowBlurRadius = 4.f; shadow.shadowOffset = NSMak
NSScrollView
中隐藏documentView
效果看起来像iBook作者:
您需要在文档视图中插入内容,以便为阴影显示留出空间,然后将视图分层并在其上设置阴影。例如:
view.wantsLayer = YES;
NSShadow *shadow = [NSShadow new];
shadow.shadowColor = [NSColor blackColor]
shadow.shadowBlurRadius = 4.f;
shadow.shadowOffset = NSMakeSize(0.f, -5.f);
view.shadow = shadow;
NSScrollView contentView是一个NSView子类,它有一个阴影字段,如果您创建一个阴影对象并将其指定给该字段,则在绘制视图时,该视图将自动显示阴影
NSShadow* shadow = [[NSShadow alloc] init];
shadow.shadowBlurRadius = 2; //set how many pixels the shadow has
shadow.shadowOffset = NSMakeSize(2, -2); //the distance from the view the shadow is dropped
shadow.shadowColor = [NSColor blackColor];
self.scrollView.contentView.shadow = shadow;
这是因为在drawRect上绘制时所有视图都使用此阴影属性,方法是使用[ShadowSet]
在绘制操作期间执行[shadow set]会使之后绘制的内容在下面复制我对输入堆栈溢出的帖子很陌生,但我也遇到了同样的问题,并且已经解决了,所以我想在搜索网络几个小时后找到解决方案,回答这个问题会很好 我的解决方案是使用以下drawRect代码为NSClipView创建一个子类
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
NSRect childRect = [[self documentView] frame];
[NSGraphicsContext saveGraphicsState];
// Create the shadow below and to the right of the shape.
NSShadow* theShadow = [[NSShadow alloc] init];
[theShadow setShadowOffset:NSMakeSize(4.0, -4.0)];
[theShadow setShadowBlurRadius:3.0];
// Use a partially transparent color for shapes that overlap.
[theShadow setShadowColor:[[NSColor grayColor]
colorWithAlphaComponent:0.95f]];
[theShadow set];
[[self backgroundColor] setFill];
NSRectFill(childRect);
// Draw your custom content here. Anything you draw
// automatically has the shadow effect applied to it.
[NSGraphicsContext restoreGraphicsState];
}
然后需要创建子类的实例,并使用setContentView选择器对其进行设置
每次内容视图大小更改时,还需要重新绘制剪辑视图。如果将内容视图设置为在用户需要时根据画布大小进行更改,则除非重新绘制剪辑视图,否则会留下一些讨厌的阴影标记
你不需要像其他人建议的那样乱搞剪辑
希望有帮助你会考虑添加一些叙述来解释这个代码为什么工作,什么使它成为问题的答案?这对提出问题的人和其他任何人都非常有帮助。