在ObjectiveC和Cocoa中以编程方式创建彩色气泡/圆 [英] Create a colored bubble/circle programmatically in ObjectiveC and Cocoa

查看:145
本文介绍了在ObjectiveC和Cocoa中以编程方式创建彩色气泡/圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以以正确的方式引导我以编程方式构建彩色气泡/圆形?

Can anyone guide me in the correct way to build a colored bubble/circle programmatically?

我无法使用图像,因为我需要它任何颜色,取决于用户的互动。

I can't use images as I need it to be able to be any color depending on user interaction.

我的想法可能是一个白色圆圈的图像,然后在它的顶部覆盖一个颜色。
但是我不知道这是否会工作,或者如何真正去处理。

My thought was maybe to make a white circle image and then overlay a color on top of it. However I am not sure if this would work, or how to really go about it.

如果有人能指出正确的方向我会很感激。

If someone could point me the right direction I would appreciate it.

推荐答案

创建一个包含NSColor作为ivar的NSView子类。在drawRect方法中,使用视图的边界创建适当大小的NSBezierPath。然后设置颜色 [myColor set] 并填充路径 [myPath fill] 。还有很多你可以做的,如设置透明度,边框等等,但我会把它留给文档,除非你有一个具体的问题。

Create an NSView subclass that holds an NSColor as an ivar. In the drawRect method, create an NSBezierPath of the appropriate size, using the view's bounds. Then set the color [myColor set] and fill the path [myPath fill]. There's a lot more you can do, such as set transparency, a border, and so on and so on, but I'll leave that to the docs unless you have a specific question.

要使用NSView子类,只需将一个视图对象拖动到您的nib,并在IB的检查器中的自定义类中选择您的子类的名称。您还需要在控制器中为其设置一个插座,以便您可以根据需要更改颜色。

To use the NSView subclass, just drag a view object onto your nib, and choose the name of your subclass in custom class in IB's inspector. You'll need to also set an outlet to it in your controller, so you can change the color as needed.

这篇关于在ObjectiveC和Cocoa中以编程方式创建彩色气泡/圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆