存在 Xib 文件的自定义绘图

发布于 2024-07-11 03:03:19 字数 778 浏览 4 评论 0原文

我有一个视图,我想添加一些自定义绘图。

我知道如何使用未连接到 Nib/Xib 文件的视图执行此操作,

  • 您可以在 -drawRect: 方法中编写绘图代码。

但是,如果我使用 -drawRect: init 视图,

[[MyView alloc] initWithNibName:@"MyView" bundle:[NSBundle mainBundle]];

当然不会被调用。 我尝试在 -viewDidLoad 中执行以下代码,

CGRect rect = [[self view] bounds];

CGContextRef ref = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ref, 2.0);
CGContextSetRGBStrokeColor(ref, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBFillColor(ref, 0, 0, 0, 0);
CGContextAddRect(ref, CGRectMake(1, 1, rect.size.width - 10, rect.size.height - 10));
CGContextStrokePath(ref);
CGContextDrawPath(ref, kCGPathFillStroke);

但没有绘制任何内容。 有任何想法吗?

I have a view that I want to add some custom drawing to.

I know how to do this with a View that isn't connected to a Nib/Xib file

  • you write the drawing code in the -drawRect: method.

But if I init the view using

[[MyView alloc] initWithNibName:@"MyView" bundle:[NSBundle mainBundle]];

-drawRect: of course doesn't get called. I tried doing the below code in -viewDidLoad

CGRect rect = [[self view] bounds];

CGContextRef ref = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ref, 2.0);
CGContextSetRGBStrokeColor(ref, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBFillColor(ref, 0, 0, 0, 0);
CGContextAddRect(ref, CGRectMake(1, 1, rect.size.width - 10, rect.size.height - 10));
CGContextStrokePath(ref);
CGContextDrawPath(ref, kCGPathFillStroke);

But nothing get drawn. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜灵血窟げ 2024-07-18 03:03:19

我认为问题在于您将视图及其视图控制器视为可互换的。 例如,没有 -[UIView initWithNibName:bundle:] 方法 - 这是一个 UIViewController 方法。

此外,视图实际上并不像绘图的“画布”。 将要求视图自行绘制; 一般来说,它不会从“外部”被吸引进来。

因此:

  1. 将 UIViewController 的子类从 MyView 重命名为 MyViewController。

  2. 创建一个名为 MyView 的新 UIView 子类。

  3. 向新的 MyView 类添加一个 -drawRect: 方法,用于执行所需的绘图。

  4. 最后,使用 Identity Inspector 将 Interface Builder 中视图控制器视图的自定义类设置为 MyView。

例如,您应该能够将其用于您的 -[MyView drawRect:] 实现:

- (void)drawRect:(CGRect)rect {
    CGRect bounds = [[self view] bounds];

    CGContextRef ref = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ref, 2.0);
    CGContextSetRGBStrokeColor(ref, 1.0, 1.0, 1.0, 1.0);
    CGContextSetRGBFillColor(ref, 0, 0, 0, 0);
    CGContextAddRect(ref, CGRectMake(1, 1, bounds.size.width - 10, bounds.size.height - 10));
    CGContextStrokePath(ref);
    CGContextDrawPath(ref, kCGPathFillStroke);
}

绘图将被剪切到传入的更新矩形。

I think the issue is that you're treating a view and its view controller as interchangeable. For example, there's no -[UIView initWithNibName:bundle:] method — that's a UIViewController method.

Furthermore, a view isn't really like a "canvas" for drawing. A view will be asked to draw itself; in general, it won't be drawn into from "outside."

So:

  1. Rename your subclass of UIViewController from MyView to MyViewController.

  2. Create a new UIView subclass named MyView.

  3. Add a -drawRect: method to your new MyView class that does the drawing you want.

  4. Finally, set the Custom Class of your view controller's view in Interface Builder to MyView using the Identity Inspector.

For example, you should be able to use this for your -[MyView drawRect:] implementation:

- (void)drawRect:(CGRect)rect {
    CGRect bounds = [[self view] bounds];

    CGContextRef ref = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ref, 2.0);
    CGContextSetRGBStrokeColor(ref, 1.0, 1.0, 1.0, 1.0);
    CGContextSetRGBFillColor(ref, 0, 0, 0, 0);
    CGContextAddRect(ref, CGRectMake(1, 1, bounds.size.width - 10, bounds.size.height - 10));
    CGContextStrokePath(ref);
    CGContextDrawPath(ref, kCGPathFillStroke);
}

The drawing will be clipped to the update rectangle passed in.

深海不蓝 2024-07-18 03:03:19

以下是三种可能的解决方案,具体取决于您的限制。

  1. 将 Xib 文件中 NSView 对象的类设置为您的自定义类之一。 您提到您知道如何使用“硬编码” NSView 子类进行自定义绘图,并且这会以相同的方式工作。 加载 Xib 文件时,NSView 将作为自定义类的对象而不是默认类加载到内存中。 使用 Interface Builder 更改 Xib 中视图的类类型非常简单。
  2. 如果 Xib 文件中的 NSView 对象已经是某种您不想覆盖的自定义视图,您可以在 Interface Builder 中向该视图添加子视图,然后简单地将子视图放入您设计的自定义类中(如#1)
  3. 您可以在运行时以编程方式将子视图添加到 Xib 文件中的 NSView 对象。 在上面的示例代码中,您可以创建自己的自定义 NSView 子类的实例,然后使用 addSubView: 方法将其添加到现有视图。

Here are three possible solutions, depending on your constraints.

  1. Set the class of the NSView object in the Xib file to be one of your custom classes. You mention that you know how to do custom drawing with a "hard-coded" NSView subclass, and this would work the same way. When the Xib file is loaded, the NSView is loaded into memory as an object of your custom class instead of the default. Changing the class type of the view in the Xib is simple with Interface Builder.
  2. If the NSView object in the Xib file is already some kind of custom view that you do not want to override, you can add a subview to that view in Interface Builder, and simply make the subview into a custom class of your design (as in #1)
  3. You can programmatically add a subview to the NSView object in the Xib file at runtime. In your sample code above, you could create an instance of your own custom NSView subclass, and then add it to the existing view using the addSubView: method.
云朵有点甜 2024-07-18 03:03:19

在一个简单的子视图中完成所有绘图并将其添加到 XIB 视图(在 viewDidLoad 中)怎么样?

How about doing all the drawing in a simple subview and add it to the XIB view (in viewDidLoad)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文