在 Apple 文档中,他们为您提供了如何将 NSBezierPath 转换为 CGPathRef 的代码。我需要反过来转换,从 CGPathRef 到 NSBezierPath。UIBezierPath 有一个名为 cgPath 的属性,所以如果我在 iPhone 上工作那不会有问题,但我在 MacOS 上工作。
这一定是一个老问题,我肯定会在互联网上找到答案,但没有运气。可能是我错过了什么。任何帮助表示赞赏。
NSBezierPath
提供了一个名为的API bounds
,它返回没有控制点的边界.是否有任何等效的API UIBezierPath
来返回除控制点之外的路径边界?
罗盘每个点的圆圈外边缘都会被裁剪掉(大概是直边框架).如何让圆圈在框架内显示?(这是通过点击按钮创建的):
在我的AppController.m中
#import "AppController.h"
#import "MakeCircle.h"
@implementation AppController
- (IBAction)makeCircle:(id)sender {
MakeCircle* newCircle = [[MakeCircle alloc] initWithFrame:NSMakeRect(100.0, 100.0, 30.0, 30.0)];
[[[[NSApplication sharedApplication] mainWindow] contentView] addSubview:newCircle];
[newCircle release];
}
@end
Run Code Online (Sandbox Code Playgroud)
在我的MakeCircle.m中
- (void)drawRect:(NSRect)rect {
[self setNeedsDisplay:YES];
[[NSColor blackColor] setStroke];
// Create our circle path
NSBezierPath* circlePath = [NSBezierPath bezierPath];
[circlePath appendBezierPathWithOvalInRect: rect];
//give the line some thickness
[circlePath setLineWidth:4];
// Outline and fill the path
[circlePath stroke];
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
我正在尝试使用剪切角创建一个NSScrollView,类似于Twitter应用程序:
我有一个NSScrollView
子类,我添加了以下代码:
- (void)drawRect:(NSRect)dirtyRect {
NSBezierPath *pcath = [NSBezierPath bezierPathWithRoundedRect:[self bounds] xRadius:kDefaultCornerRadius yRadius:kDefaultCornerRadius];
[path setClip];
[super drawRect:dirtyRect];
}
Run Code Online (Sandbox Code Playgroud)
我希望它的内容NSScrollView
有圆角,但它不符合剪切路径.我怎样才能做到这一点?
更新和澄清
我知道如何制作自定义NSScroller
,我知道如何使其透明覆盖.我要问的是如何使NSSCrollView
剪辑成为角落,包括它包含的所有内容.该NSScrollView
是内部的NSView
,其有可能会改变,这意味着视图叠加假冒圆角的背景是不是一种选择.
我有奇怪的问题,与这个问题的答案有关:
我将此代码用于自定义视图的drawRect方法.我有这个:
- (void)drawRect:(NSRect)rect
{
// Create and fill the shown path
NSBezierPath *path = [NSBezierPath
bezierPathWithRoundedRect:[self bounds]
xRadius:4.0f
yRadius:4.0f];
[[NSColor colorWithCalibratedWhite:0.8f alpha:0.2f] set];
[path fill];
// Save the graphics state for shadow
[NSGraphicsContext saveGraphicsState];
// Set the shown path as the clip
[path setClip];
// Create and stroke the shadow
NSShadow * shadow = [[[NSShadow alloc] init] autorelease];
[shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0f alpha:0.8f]];
[shadow setShadowBlurRadius:2.0];
[shadow set];
[path stroke];
// Restore the graphics state
[NSGraphicsContext restoreGraphicsState];
if ( highlight …
Run Code Online (Sandbox Code Playgroud) 使用我的应用程序时,我遇到了一些绘画方面的问题:
我想知道为什么为什么要回到Obj-c -moveToPoint()
并-lineToPoint()
没有问题地绘制所有东西,而现在却迅速地看到所有东西都一样,只是我的视线出现了一个奇怪的边界。让我更好地解释一下:
想象一下,您的任务是从A(0,0)到B(10,10)画一条基本线。
我们都知道如何在obj-c中做到这一点,但是很快我就有了新的东西:
var path : NSBezierPath = NSBezierPath(rect: dirtyRect)
let color = NSColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
color.set()
path.moveToPoint(NSPoint(x: 0,y: 0))
path.lineToPoint(NSPoint(x: 10,y: 10))
path.lineWidth = 5.0 //Makes the line 5px width, but it even
//creates an annoying border along the view
//Try it out, i can't figure out how to get rid of it
path.stroke()
Run Code Online (Sandbox Code Playgroud) 我想创建一个像素大小的点。我假设这样做的方法是使用 NSBezierPath 画一条线,起点等于终点(参见下面的代码),但这样做会产生一个完全空的窗口。
func drawPoint() {
let path = NSBezierPath()
NSColor.blue.set()
let fromPoint = NSMakePoint(CGFloat(100) , CGFloat(100))
let toPoint = NSMakePoint(CGFloat(100) , CGFloat(100))
path.move(to: fromPoint)
path.line(to: toPoint)
path.lineWidth = 1.0
path.stroke()
path.fill()
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我将 toPoint 坐标从 100,100 更改为 101,101(或 100,100 以外的任何坐标集),则会显示蓝色形状。有谁知道为什么会这样?
我发现的另一个问题是,如果您将 toPoint 的坐标设置为等于(例如)101,101,那么它将创建一个长度为两个像素的正方形,但还会添加另一个像素长度的模糊层(请参见下图,放大为细节)。
然而,我想做的只是放置一个像素大小的单个蓝色小方块,没有模糊。有谁知道我该怎么做?
我很好奇我是否可以使用NSBezierPath绘制NSRect然后用一些条纹图案填充它,所以它看起来像下图?
任何形式的帮助都非常感谢!
nsbezierpath ×8
cocoa ×6
macos ×2
swift ×2
cgpathref ×1
drawing ×1
nsrect ×1
nsscrollview ×1
nsshadow ×1
nsview ×1
objective-c ×1
subclass ×1