ios 如何绘制一个只是一个圆圈的自定义 UIView - iPhone 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6589265/
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
How to draw a custom UIView that is just a circle - iPhone app
提问by StanLe
How would I go about drawing a custom UIView that is literally just a ball (a 2D circle)? Would I just override the drawRect method? And can someone show me the code for drawing a blue circle?
我将如何绘制一个实际上只是一个球(一个 2D 圆)的自定义 UIView?我会覆盖 drawRect 方法吗?有人可以告诉我绘制蓝色圆圈的代码吗?
Also, would it be okay to change the frame of that view within the class itself? Or do I need to change the frame from a different class?
另外,是否可以在类本身中更改该视图的框架?还是我需要从不同的类更改框架?
(just trying to set up a ball bouncing around)
(只是试图设置一个弹跳的球)
回答by Kal
You could use QuartzCore and do something this --
你可以使用 QuartzCore 并做一些这样的事情——
self.circleView = [[UIView alloc] initWithFrame:CGRectMake(10,20,100,100)];
self.circleView.alpha = 0.5;
self.circleView.layer.cornerRadius = 50; // half the width/height
self.circleView.backgroundColor = [UIColor blueColor];
回答by Steve
Would I just override the drawRect method?
我会覆盖 drawRect 方法吗?
Yes:
是的:
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(ctx, rect);
CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor blueColor] CGColor]));
CGContextFillPath(ctx);
}
Also, would it be okay to change the frame of that view within the class itself?
另外,是否可以在类本身中更改该视图的框架?
Ideally not, but you could.
理想情况下不是,但你可以。
Or do I need to change the frame from a different class?
还是我需要从不同的类更改框架?
I'd let the parent control that.
我会让父母控制的。
回答by Gintama
Here is another way by using UIBezierPath (maybe it's too late ^^) Create a circle and mask UIView with it, as follows:
这里还有一个方法是使用UIBezierPath(可能太晚了^^)创建一个圆并用它来遮罩UIView,如下:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
view.backgroundColor = [UIColor blueColor];
CAShapeLayer *shape = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:view.center radius:(view.bounds.size.width / 2) startAngle:0 endAngle:(2 * M_PI) clockwise:YES];
shape.path = path.CGPath;
view.layer.mask = shape;
回答by Kevin ABRIOUX
My contribution with a Swift extension:
我对 Swift 扩展的贡献:
extension UIView {
func asCircle() {
self.layer.cornerRadius = self.frame.width / 2;
self.layer.masksToBounds = true
}
}
Just call myView.asCircle()
打电话就行 myView.asCircle()
回答by Maciej
Swift 3- custom class, easy to reuse. It uses backgroundColor
set in UI builder
Swift 3- 自定义类,易于重用。它backgroundColor
在 UI 构建器中使用set
import UIKit
@IBDesignable
class CircleBackgroundView: UIView {
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = bounds.size.width / 2
layer.masksToBounds = true
}
}
回答by Vyacheslav
Swift 3class:
斯威夫特 3类:
import UIKit
class CircleView: UIView {
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else {return}
context.addEllipse(in: rect)
context.setFillColor(.blue.cgColor)
context.fillPath()
}
}
回答by matrix3003
Another way of approaching circle (and other shapes) drawing is by using masks. You draw circles or other shapes by, first, making masks of the shapes you need, second, provide squares of your color and, third, apply masks to those squares of color. You can change either mask or color to get a new custom circle or other shape.
另一种绘制圆形(和其他形状)的方法是使用蒙版。您可以通过以下方式绘制圆形或其他形状:首先,制作所需形状的蒙版,其次,提供您的颜色方块,第三,将蒙版应用到这些颜色方块上。您可以更改蒙版或颜色以获得新的自定义圆形或其他形状。
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *area1;
@property (weak, nonatomic) IBOutlet UIView *area2;
@property (weak, nonatomic) IBOutlet UIView *area3;
@property (weak, nonatomic) IBOutlet UIView *area4;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.area1.backgroundColor = [UIColor blueColor];
[self useMaskFor: self.area1];
self.area2.backgroundColor = [UIColor orangeColor];
[self useMaskFor: self.area2];
self.area3.backgroundColor = [UIColor colorWithRed: 1.0 green: 0.0 blue: 0.5 alpha:1.0];
[self useMaskFor: self.area3];
self.area4.backgroundColor = [UIColor colorWithRed: 1.0 green: 0.0 blue: 0.5 alpha:0.5];
[self useMaskFor: self.area4];
}
- (void)useMaskFor: (UIView *)colorArea {
CALayer *maskLayer = [CALayer layer];
maskLayer.frame = colorArea.bounds;
UIImage *maskImage = [UIImage imageNamed:@"cirMask.png"];
maskLayer.contents = (__bridge id)maskImage.CGImage;
colorArea.layer.mask = maskLayer;
}
@end
Here is the output of the code above:
这是上面代码的输出:
回答by user8675
There's another alternative for lazy people. You can set the layer.cornerRadius
key pathfor your view in the Interface Builder. For example, if your view has a width = height of 48, set layer.cornerRadius = 24
:
对于懒惰的人,还有另一种选择。您可以在Interface Builder 中为您的视图设置layer.cornerRadius
键路径。例如,如果您的视图的width = height 为 48,请设置:layer.cornerRadius = 24
However, this only works if you have a static size of the view (width/height is fixed)
and it's not showing the circle in the interface builder.
但是,这只适用于视图的静态大小(width/height is fixed)
并且它没有在界面构建器中显示圆圈。
回答by Lucas Chwe
The best is using Core Graphics like Steve said, especially if you want your circle to look smooth and polished. Here its the solution in Swift:
最好的方法是使用 Steve 所说的 Core Graphics,特别是如果你想让你的圆圈看起来光滑和抛光。这是 Swift 中的解决方案:
override func draw(_ rect: CGRect) {
if let context = UIGraphicsGetCurrentContext() {
context.addEllipse(in: rect)
context.setFillColor(UIColor.blue.cgColor)
context.fillPath()
}
}
回答by Mohammad Razipour
Swift 3 - Xcode 8.1
斯威夫特 3 - Xcode 8.1
@IBOutlet weak var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let size:CGFloat = 35.0
myView.bounds = CGRect(x: 0, y: 0, width: size, height: size)
myView.layer.cornerRadius = size / 2
myView.layer.borderWidth = 1
myView.layer.borderColor = UIColor.Gray.cgColor
}