如何绘制一个只是一个圆圈的自定义UIView – iPhone应用程序

我将如何去绘制一个实际上只是一个球(2D圈)的自定义UIView? 我会重写drawRect方法吗? 有人能告诉我画蓝色圆圈的代码吗?

另外,在类本身中改变视图的框架可以吗? 还是我需要改变不同类别的框架?

(试图设置一个弹跳的球)

你可以使用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]; 

我会重写drawRect方法吗?

是:

 - (void)drawRect:(CGRect)rect { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextAddEllipseInRect(ctx, rect); CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor blueColor] CGColor])); CGContextFillPath(ctx); } 

另外,在类本身中改变视图的框架可以吗?

理想情况下,但你可以。

还是我需要改变不同类别的框架?

我会让父母控制。

这是通过使用UIBezierPath的另一种方式(也许它已经太晚了^^)创build一个圆圈并用它掩盖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; 

我的迅速扩展贡献:

 extension UIView{ func asCircle(){ self.layer.cornerRadius = self.frame.width / 2; self.layer.masksToBounds = true } } 

只需调用myView.asCircle()

在这里输入图像描述 接近圆(和其他形状)绘图的另一种方式是使用掩模。 通过先画出你需要的形状的面具,然后画出你的颜色的正方形,然后在这些正方形的颜色上涂抹面具,你可以绘制圆形或其他形状。 您可以更改蒙版或颜色以获得新的自定义圆形或其他形状。 以上是以下代码的输出。

 #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 

Swift 3class:

 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() } } 

Swift 3 – 自定义类,易于重用。 它使用在UI构build器中设置的backgroundColor

 import UIKit @IBDesignable class CircleBackgroundView: UIView { override func layoutSubviews() { super.layoutSubviews() layer.cornerRadius = bounds.size.width / 2; layer.masksToBounds = true; } } 

Swift 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 }