UIView圆角问题

发布于 2024-08-23 12:25:55 字数 2057 浏览 2 评论 0原文

这段代码有什么问题吗?

-(void) drawRect:(CGRect) rect {
        CGContextRef c = UIGraphicsGetCurrentContext();
        if (c != nil)  {
            CGContextSetFillColorWithColor(c, self.cornerColor.CGColor);
            [self drawRoundedCornersInRect:self.bounds inContext:c];
            CGContextFillPath(c);
        }
    }

    -(void) drawCornerInContext:(CGContextRef)c cornerX:(int) x cornerY:(int) y
            arcEndX:(int) endX arcEndY:(int) endY {
        CGContextMoveToPoint(c, x, endY);
        CGContextAddArcToPoint(c, x, y, endX, y, radius);
        CGContextAddLineToPoint(c, x, y);
        CGContextAddLineToPoint(c, x, endY);
    }

    -(void) drawRoundedCornersInRect:(CGRect) rect inContext:(CGContextRef) c  {
     int x_left = rect.origin.x;
     int x_left_center = rect.origin.x + radius;
     int x_right_center = rect.origin.x + rect.size.width - radius;
     int x_right = rect.origin.x + rect.size.width;
     int y_top = rect.origin.y;
     int y_top_center = rect.origin.y + radius;
     int y_bottom_center = rect.origin.y + rect.size.height - radius;
     int y_bottom = rect.origin.y + rect.size.height;

        if (roundUpperLeft) {
            [self drawCornerInContext:c cornerX: x_left cornerY: y_top
                  arcEndX: x_left_center arcEndY: y_top_center];
        }

        if (roundUpperRight) {
            [self drawCornerInContext:c cornerX: x_right cornerY: y_top
                  arcEndX: x_right_center arcEndY: y_top_center];
        }

        if (roundLowerRight) {
            [self drawCornerInContext:c cornerX: x_right cornerY: y_bottom
                  arcEndX: x_right_center arcEndY: y_bottom_center];
        }

        if (roundLowerLeft) {
            [self drawCornerInContext:c cornerX: x_left cornerY: y_bottom
                  arcEndX: x_left_center arcEndY: y_bottom_center];
        }
    }

没有错误,没有警告......但圆角根本不起作用。我在此处找到了这段代码。

Whats wrong with this code?

-(void) drawRect:(CGRect) rect {
        CGContextRef c = UIGraphicsGetCurrentContext();
        if (c != nil)  {
            CGContextSetFillColorWithColor(c, self.cornerColor.CGColor);
            [self drawRoundedCornersInRect:self.bounds inContext:c];
            CGContextFillPath(c);
        }
    }

    -(void) drawCornerInContext:(CGContextRef)c cornerX:(int) x cornerY:(int) y
            arcEndX:(int) endX arcEndY:(int) endY {
        CGContextMoveToPoint(c, x, endY);
        CGContextAddArcToPoint(c, x, y, endX, y, radius);
        CGContextAddLineToPoint(c, x, y);
        CGContextAddLineToPoint(c, x, endY);
    }

    -(void) drawRoundedCornersInRect:(CGRect) rect inContext:(CGContextRef) c  {
     int x_left = rect.origin.x;
     int x_left_center = rect.origin.x + radius;
     int x_right_center = rect.origin.x + rect.size.width - radius;
     int x_right = rect.origin.x + rect.size.width;
     int y_top = rect.origin.y;
     int y_top_center = rect.origin.y + radius;
     int y_bottom_center = rect.origin.y + rect.size.height - radius;
     int y_bottom = rect.origin.y + rect.size.height;

        if (roundUpperLeft) {
            [self drawCornerInContext:c cornerX: x_left cornerY: y_top
                  arcEndX: x_left_center arcEndY: y_top_center];
        }

        if (roundUpperRight) {
            [self drawCornerInContext:c cornerX: x_right cornerY: y_top
                  arcEndX: x_right_center arcEndY: y_top_center];
        }

        if (roundLowerRight) {
            [self drawCornerInContext:c cornerX: x_right cornerY: y_bottom
                  arcEndX: x_right_center arcEndY: y_bottom_center];
        }

        if (roundLowerLeft) {
            [self drawCornerInContext:c cornerX: x_left cornerY: y_bottom
                  arcEndX: x_left_center arcEndY: y_bottom_center];
        }
    }

No errors, No warnings ... but the round corners don't work at all. I found this code here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画▽骨i 2024-08-30 12:25:55

您需要开始一条路径,并在完成后关闭它。我认为你对 CGContextAddLineToPoint 的第二次调用把事情搞砸了。这是一个有效的片段。研究它并增强它以支持您的多种情况(似乎您希望能够只圆化某些角,而不一定是所有角......)

void addRoundedRect(CGContextRef ctx, CGRect rect, float cornerRadius) {
    if (cornerRadius <= 2.0) {
        CGContextAddRect(ctx, rect);
    } else {
        float x_left = rect.origin.x;
        float x_left_center = x_left + cornerRadius;
        float x_right_center = x_left + rect.size.width - cornerRadius;
        float x_right = x_left + rect.size.width;
        float y_top = rect.origin.y;
        float y_top_center = y_top + cornerRadius;
        float y_bottom_center = y_top + rect.size.height - cornerRadius;
        float y_bottom = y_top + rect.size.height;
        /* Begin path */
        CGContextBeginPath(ctx);
        CGContextMoveToPoint(ctx, x_left, y_top_center);
        /* First corner */
        CGContextAddArcToPoint(ctx, x_left, y_top, x_left_center, y_top, cornerRadius);
        CGContextAddLineToPoint(ctx, x_right_center, y_top);
        /* Second corner */
        CGContextAddArcToPoint(ctx, x_right, y_top, x_right, y_top_center, cornerRadius);
        CGContextAddLineToPoint(ctx, x_right, y_bottom_center);
        /* Third corner */
        CGContextAddArcToPoint(ctx, x_right, y_bottom, x_right_center, y_bottom, cornerRadius);
        CGContextAddLineToPoint(ctx, x_left_center, y_bottom);
        /* Fourth corner */
        CGContextAddArcToPoint(ctx, x_left, y_bottom, x_left, y_bottom_center, cornerRadius);
        CGContextAddLineToPoint(ctx, x_left, y_top_center);
        /* Done */
        CGContextClosePath(ctx);
    }
}

You need to begin a path, and close it when you're done. Your 2nd call to CGContextAddLineToPoint messes things up I think. Here's a snippet that works. Study it and enhance it to support your multiple cases (it seems that you want to be able to round only some corners, and not necessarily all of them...)

void addRoundedRect(CGContextRef ctx, CGRect rect, float cornerRadius) {
    if (cornerRadius <= 2.0) {
        CGContextAddRect(ctx, rect);
    } else {
        float x_left = rect.origin.x;
        float x_left_center = x_left + cornerRadius;
        float x_right_center = x_left + rect.size.width - cornerRadius;
        float x_right = x_left + rect.size.width;
        float y_top = rect.origin.y;
        float y_top_center = y_top + cornerRadius;
        float y_bottom_center = y_top + rect.size.height - cornerRadius;
        float y_bottom = y_top + rect.size.height;
        /* Begin path */
        CGContextBeginPath(ctx);
        CGContextMoveToPoint(ctx, x_left, y_top_center);
        /* First corner */
        CGContextAddArcToPoint(ctx, x_left, y_top, x_left_center, y_top, cornerRadius);
        CGContextAddLineToPoint(ctx, x_right_center, y_top);
        /* Second corner */
        CGContextAddArcToPoint(ctx, x_right, y_top, x_right, y_top_center, cornerRadius);
        CGContextAddLineToPoint(ctx, x_right, y_bottom_center);
        /* Third corner */
        CGContextAddArcToPoint(ctx, x_right, y_bottom, x_right_center, y_bottom, cornerRadius);
        CGContextAddLineToPoint(ctx, x_left_center, y_bottom);
        /* Fourth corner */
        CGContextAddArcToPoint(ctx, x_left, y_bottom, x_left, y_bottom_center, cornerRadius);
        CGContextAddLineToPoint(ctx, x_left, y_top_center);
        /* Done */
        CGContextClosePath(ctx);
    }
}
紅太極 2024-08-30 12:25:55

我使用这个:[view.layer setCornerRadius:7];。它使用 UIViewCALayer backer 设置角半径。

I use this: [view.layer setCornerRadius:7];. It sets the corner radius using the UIView's CALayer backer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文