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

Sta*_*nLe 128 iphone uiview ipad ios

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

此外,在类本身内更改该视图的框架是否可以?或者我是否需要从不同的班级更改框架?

(只是试图设置一个球弹跳)

Kal*_*Kal 207

您可以使用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];
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考,为了使您的视图成为使用QuartCore的圆形,您的角半径需要是框架高度/宽度的一半.这是制作圆圈的最简单方法,但不一定是效率最高的.如果性能至关重要,drawRect可能会产生更好的结果. (101认同)
  • 它是.100是高度/宽度. (4认同)
  • 是的,抱歉没有指示你,Kal.我提到StanLe,以防他想使用不同大小的视图. (3认同)

Ste*_*eve 133

我会覆盖drawRect方法吗?

是:

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

此外,在类本身内更改该视图的框架是否可以?

理想情况下不是,但你可以.

或者我是否需要从不同的班级更改框架?

我让父母控制那个.

  • @gaussblurinc使用`CGContextSetFillColorWithColor(ctx,self.colorOfCircle.CGColor);`,解决方案`CGColorGetComponents`中提出的方法仅适用于某些颜色,请参阅http://stackoverflow.com/questions/9238743/is-there-an -issue与 - cgcolorgetcomponents (9认同)

Gin*_*ama 31

这是使用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;
Run Code Online (Sandbox Code Playgroud)


Kev*_*OUX 24

我对快速扩展的贡献:

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

打电话吧 myView.asCircle()


Mac*_*wia 15

Swift 3 - 自定义类,易于重用.它使用backgroundColorUI构建器中的set

import UIKit

@IBDesignable
class CircleBackgroundView: UIView {

    override func layoutSubviews() {
        super.layoutSubviews()
        layer.cornerRadius = bounds.size.width / 2
        layer.masksToBounds = true
    }

}
Run Code Online (Sandbox Code Playgroud)


Vya*_*lav 14

Swift 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()
    }           
}
Run Code Online (Sandbox Code Playgroud)


mat*_*003 6

在此输入图像描述接近圆形(和其他形状)绘图的另一种方法是使用蒙版.你绘制圆形或其他形状,首先,制作你需要的形状的面具,其次,提供你的颜色的正方形,第三,将面具应用于那些颜色的正方形.您可以更改蒙版或颜色以获得新的自定义圆形或其他形状.以上是下面代码的输出.

#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
Run Code Online (Sandbox Code Playgroud)