con*_*are 15 cocoa clipping rounded-corners nsview nsbezierpath
我正在创建一个NSView具有圆角的子类.此视图是一个容器,其他子视图将添加到其中.我试图获得圆角NSView以夹住所有子视图的角落,但我无法得到它.
- (void)drawRect:(NSRect)dirtyRect {
NSRect rect = [self bounds];
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:self.radius yRadius:self.radius];
[path addClip];
[[NSColor redColor] set];
NSRectFill(dirtyRect);
[super drawRect:dirtyRect];
}
Run Code Online (Sandbox Code Playgroud)
红色就是例如.如果我在矩形中添加子视图,则角落不会被剪裁:

我怎样才能做到这一点?
Jas*_*wig 31
使用核心动画图层将正确剪辑子图层.
在您的容器NSView子类中:
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer = _layer; // strangely necessary
self.wantsLayer = YES;
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 10.0;
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
小智 0
您尝试过用图层进行剪辑吗?
self.layer.cornerRadius = self.radius;
self.layer.masksToBounds = YES;
啊,抱歉,不知怎的,我错过了你在谈论 NSView,而不是 UIView。在所有情况下都很难剪切 NSView 子视图,因为似乎大多数 Cocoa 标准视图都设置了自己的剪切路径。使用一些填充来布局子视图可能会更容易,并且避免了剪裁的需要。
| 归档时间: |
|
| 查看次数: |
14583 次 |
| 最近记录: |