Date
Sep. 8th, 2024
 
2024年 8月 6日

Post: iOS: UIView 绘制圆角矩形

iOS: UIView 绘制圆角矩形

Published 12:11 Nov 16, 2014.

Created by @ezra. Categorized in #Programming, and tagged as #iOS.

Source format: Markdown

Table of Content

绘制圆角矩形:

@implementation UIView (Draw)

void CGContextAddRoundRect(CGContextRef __nullable c, CGRect rect, CGFloat radius) {
    CGFloat width = rect.size.width;
    CGFloat x = rect.origin.x;
    CGFloat height = rect.size.height;
    CGFloat y = rect.origin.y;

    // 移动到初始点
    CGContextMoveToPoint(c, radius + x, y);

    // 绘制第1条线和第1个1/4圆弧
    CGContextAddLineToPoint(c, width - radius + x, y);
    CGContextAddArc(c, width - radius + x, radius + y, radius, -0.5 * M_PI, 0.0, 0);

    // 绘制第2条线和第2个1/4圆弧
    CGContextAddLineToPoint(c, width + x, height - radius + y);
    CGContextAddArc(c, width - radius + x, height - radius + y, radius, 0.0, 0.5 * M_PI, 0);

    // 绘制第3条线和第3个1/4圆弧
    CGContextAddLineToPoint(c, radius + x, height + y);
    CGContextAddArc(c, radius + x, height - radius + y, radius, 0.5 * M_PI, M_PI, 0);

    // 绘制第4条线和第4个1/4圆弧
    CGContextAddLineToPoint(c, x, radius + y);
    CGContextAddArc(c, radius + x, radius + y, radius, M_PI, 1.5 * M_PI, 0);
}

- (void)drawRoundRectWithContext:(CGContextRef __nullable)context inRect:(CGRect)rect radius:(CGFloat)radius {
    CGContextAddRoundRect(context, rect, radius);
}
Pinned Message
HOTODOGO
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
程序猿、设计师、奇怪的博主。
苹果死忠、热爱色彩斑斓的世界。
如有意向请随时 与我联系