objective-c 在 ObjectiveC 和 Cocoa 中以编程方式创建彩色气泡/圆圈
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/354610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a colored bubble/circle programmatically in ObjectiveC and Cocoa
提问by kdbdallas
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.
如果有人能指出我正确的方向,我将不胜感激。
采纳答案by Marc Charbonneau
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 子类,将 NSColor 作为 ivar。在 drawRect 方法中,使用视图的边界创建一个适当大小的 NSBezierPath。然后设置颜色[myColor set]并填充路径[myPath fill]。您还可以做很多事情,例如设置透明度、边框等等,但除非您有特定问题,否则我会将其留给文档。
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.
要使用 NSView 子类,只需将一个视图对象拖到您的笔尖上,然后在 IB 的检查器中的自定义类中选择您的子类的名称。您还需要在控制器中为其设置一个插座,以便您可以根据需要更改颜色。
回答by joshperry
There are a couple steps to drawing something in Cocoa.
在 Cocoa 中绘制一些东西有几个步骤。
First you need a path that will be used to define the object that you are going to be drawing. Take a look here Drawing Fundamental Shapesfor a guide on creating paths in Cocoa. You will be most interested in sending the "appendBezierPathWithOvalInRect" message to an "NSBezierPath" object, this takes a rectangle that bounds the circle you want to draw.
首先,您需要一个用于定义要绘制的对象的路径。在此处查看绘制基本形状以获取在 Cocoa 中创建路径的指南。您最感兴趣的是将“appendBezierPathWithOvalInRect”消息发送到“NSBezierPath”对象,这需要一个矩形来限定您要绘制的圆。
This code will create a 10x10 circle at coordinates 10,10:
此代码将在坐标 10,10 处创建一个 10x10 的圆:
NSRect rect = NSMakeRect(10, 10, 10, 10);
NSBezierPath* circlePath = [NSBezierPath bezierPath];
[circlePath appendBezierPathWithOvalInRect: rect];
Once you have your path you want to set the color for the current drawing context. There are two colors, stroke and fill; stroke is the outline of the path and the fill is the interior color. To set a color you send "set" to an "NSColor" object.
一旦你有了你想要为当前绘图上下文设置颜色的路径。有两种颜色,描边和填充;stroke 是路径的轮廓,fill 是内部颜色。要设置颜色,您将“设置”发送到“NSColor”对象。
This sets the stroke to black and the fill to red:
这将笔触设置为黑色,将填充设置为红色:
[[NSColor blackColor] setStroke];
[[NSColor redColor] setFill];
Now that you have your path and you have your colors set just fill the path and then draw it:
现在你有了你的路径并且你已经设置了颜色,只需填充路径然后绘制它:
[path stroke];
[path fill];
All of this will need to be done in a graphics context like in drawRect of a view perhaps. All of this together with a graphics context would look like this:
所有这些都需要在图形上下文中完成,例如在视图的 drawRect 中。所有这些与图形上下文一起看起来像这样:
- (void)drawRect:(NSRect)rect
{
// Get the graphics context that we are currently executing under
NSGraphicsContext* gc = [NSGraphicsContext currentContext];
// Save the current graphics context settings
[gc saveGraphicsState];
// Set the color in the current graphics context for future draw operations
[[NSColor blackColor] setStroke];
[[NSColor redColor] setFill];
// Create our circle path
NSRect rect = NSMakeRect(10, 10, 10, 10);
NSBezierPath* circlePath = [NSBezierPath bezierPath];
[circlePath appendBezierPathWithOvalInRect: rect];
// Outline and fill the path
[circlePath stroke];
[circlePath fill];
// Restore the context to what it was before we messed with it
[gc restoreGraphicsState];
}
回答by Almas Adilbek
You may use simple UIViewto create perfectcircle with only parameter radius:
您可以使用 simpleUIView来创建只有参数的完美圆radius:
// Add framework CoreGraphics.framework
#import <QuartzCore/QuartzCore.h>
-(UIView *)circleWithColor:(UIColor *)color radius:(int)radius {
UIView *circle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 2 * radius, 2 * radius)];
circle.backgroundColor = color;
circle.layer.cornerRadius = radius;
circle.layer.masksToBounds = YES;
return circle;
}
回答by Sorted
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(c, 40, 0, 255, 0.1);
CGContextSetRGBStrokeColor(c, 0, 40, 255, 0.5);
// Draw a green solid circle
CGContextSetRGBFillColor(c, 0, 255, 0, 1);
CGContextFillEllipseInRect(c, CGRectMake(100, 100, 25, 25));
回答by Stephan Eggermont
Download sketch from apple. http://developer.apple.com/library/mac/#samplecode/Sketch
从苹果下载草图。http://developer.apple.com/library/mac/#samplecode/Sketch
It can do a lot more, but one of the things is draw circles.
它可以做更多的事情,但其中一件事就是画圆圈。

