【问题标题】:UIView rounded corners QuestionUIView圆角问题
【发布时间】:2011-01-24 00:15:42
【问题描述】:

这段代码有什么问题?

-(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];
        }
    }

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

【问题讨论】:

    标签: iphone uiview rounded-corners


    【解决方案1】:

    我用这个:[view.layer setCornerRadius:7];。它使用UIView 的CALayer 支持器设置圆角半径。

    【讨论】:

    • 我想我不清楚。我想指定哪个角将被圆角。使用 CALayercornerRadius 我做不到。
    • 设置圆角半径和边框以查看示例cocoabugs.blogspot.com/2010/08/…
    【解决方案2】:

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

    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);
        }
    }
    

    【讨论】:

    猜你喜欢
    • 2011-08-05
    • 2010-12-03
    • 2013-05-15
    • 2016-01-21
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多