我该如何去绘制一个自定义的 UIView,它实际上只是一个球(一个2D 圆) ?我是否可以重写 draRect 方法?谁能告诉我画蓝色圆圈的代码?
另外,在类本身内更改视图的框架是否可以?或者我需要从不同的类改变帧?
(只是试图建立一个球弹来弹去)
我能不能改变抽屉 方法?
是的:
- (void)drawRect:(CGRect)rect { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextAddEllipseInRect(ctx, rect); CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor blueColor] CGColor])); CGContextFillPath(ctx); }
另外,在类本身内更改视图的框架是否可以?
最好不要,但你可以。
或者我需要从不同的类改变帧?
我会让家长来控制。
你可以用石英核来做这个
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];
下面是使用 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;
接近圆形(和其他形状)绘图的另一种方法是使用蒙版。 你画圆圈或其他形状,首先,制作你需要的形状的面具,其次,提供你的颜色的正方形,第三,应用面具的颜色的正方形。您可以更改蒙版或颜色以获得新的自定义圆或其他形状。
#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
以下是上述代码的输出:
我的贡献:
extension UIView { func asCircle() { self.layer.cornerRadius = self.frame.width / 2; self.layer.masksToBounds = true } }
打电话给 myView.asCircle()
myView.asCircle()
斯威夫特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 }
斯威夫特3班:
import UIKit class CircleView: UIView { override func draw(_ rect: CGRect) { guard let context = UIGraphicsGetCurrentContext() else {return} context.addEllipse(in: rect) context.setFillColor(UIColor.blue.cgColor) context.fillPath() } }
Swift 3 -自定义类,易于重用
import UIKit @IBDesignable class CircleBackgroundView: UIView { override func layoutSubviews() { super.layoutSubviews() layer.cornerRadius = bounds.size.width / 2 layer.masksToBounds = true } }
懒人还有另一种选择。您可以在 Interface Builder中为您的视图设置 layer.cornerRadius 键路径。例如,如果您的视图有一个 宽度 = 高度48,设置为 layer.cornerRadius = 24:
layer.cornerRadius
layer.cornerRadius = 24
但是,只有当你的视图是静态的,而且没有在 Interface Builder 中显示圆的时候,这种方法才有效。