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)
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)
此外,在类本身内更改该视图的框架是否可以?
理想情况下不是,但你可以.
或者我是否需要从不同的班级更改框架?
我让父母控制那个.
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)
接近圆形(和其他形状)绘图的另一种方法是使用蒙版.你绘制圆形或其他形状,首先,制作你需要的形状的面具,其次,提供你的颜色的正方形,第三,将面具应用于那些颜色的正方形.您可以更改蒙版或颜色以获得新的自定义圆形或其他形状.以上是下面代码的输出.
#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)