本文介绍了如何快速绘制简单的圆角矩形(圆角)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我设法绘制了一个矩形:-)但是我不知道如何绘制一个圆角的矩形。
I managed to draw a rect :-) But I don't know how to draw a rounded rect.
有人可以通过以下代码帮助我吗?
Can someone help me out with the following code how to round the rect?
let canvas = UIGraphicsGetCurrentContext()
rec = CGRectMake(0, 0, 40, 40);
//var maskPath = UIBezierPath(roundedRect: rec, byRoundingCorners: .BottomLeft | .BottomRight, cornerRadii: CGSize(width: 3, height: 3))
CGContextAddRect(canvas, rec);
CGContextFillPath(canvas);
推荐答案
///将此代码放入ur drawRect
//Put this code in ur drawRect
目标-C
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGPathRef clippath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(x,y, width, height) cornerRadius:6].CGPath;
CGContextAddPath(ctx, clippath);
CGContextSetFillColorWithColor(ctx, self.color.CGColor);
CGContextClosePath(ctx);
CGContextFillPath(ctx);
[self.color set];
[_path closePath]; // Implicitly does a line between p4 and p1
[_path fill]; // If you want it filled, or...
[_path stroke]; // ...if you want to draw the outline.
CGContextRestoreGState(ctx);
}
Swift 3
func drawRect(rect : CGRect)
{
// Size of rounded rectangle
let rectWidth = rect.width
let rectHeight = rect.height
// Find center of actual frame to set rectangle in middle
let xf:CGFloat = (self.frame.width - rectWidth) / 2
let yf:CGFloat = (self.frame.height - rectHeight) / 2
let ctx: CGContext = UIGraphicsGetCurrentContext()!
ctx.saveGState()
let rect = CGRect(x: xf, y: yf, width: rectWidth, height: rectHeight)
let clipPath: CGPath = UIBezierPath(roundedRect: rect, cornerRadius: rectCornerRadius).cgPath
ctx.addPath(clipPath)
ctx.setFillColor(rectBgColor.cgColor)
ctx.closePath()
ctx.fillPath()
ctx.restoreGState()
}
这篇关于如何快速绘制简单的圆角矩形(圆角)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!