iphone - 绘制矩形中的不正确边框

标签 iphone ios drawrect uibezierpath

我试图在 drawRect 方法中使用贝塞尔曲线路径在矩形上绘制圆角,但不知何故圆角显示在矩形的内侧,而不是内外两侧。代码如下。附上目前正在绘制的边框(边框外侧未圆角)

Image with Inner rounded corners only

 - (void)drawRect:(CGRect)rect
 {


  // Drawing code
    CGContextRef context=UIGraphicsGetCurrentContext();

    //Set gray color to whole context
    [[UIColor lightGrayColor] set];
    CGContextSetAlpha(context,0.7);
    UIRectFill(rect);

    // Configure the context colors and line
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:131./255. green:148./255. blue:219./255. alpha:1.0].CGColor);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 10.0);
    CGSize size=self.bounds.size;

    CGFloat radius = 10.0;

    CGSize guideSize=CGSizeMake(330, 130);
    CGRect guideCoords= CGRectMake(size.width*.5-guideSize.width*.5, size.height*.5-guideSize.height*.5, guideSize.width , guideSize.height);

    // Guide drawing
    CGContextStrokeRect(context,guideCoords);

    // Draw the Text
    [kVinGuideMessage drawInRect:CGRectMake(guideCoords.origin.x+kSideMessageMargin, guideCoords.origin.y+guideSize.height+kMarginFromGuide, guideCoords.size.width-2*kSideMessageMargin,40) withFont:[UIFont systemFontOfSize:16.0] lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];


    //Get instersection and clear color of inner overlay
    CGRect holeRectIntersection = CGRectIntersection(rect,guideCoords);

    //----------ADDING ROUNDED CORNERS HERE-----------//
    CGPathRef clippath = [UIBezierPath bezierPathWithRoundedRect:guideCoords cornerRadius:radius].CGPath;
    CGContextAddPath(context, clippath);
    CGContextClip(context);
    //------------------------------------------------//
    [[UIColor clearColor] setFill];
    UIRectFill(holeRectIntersection);

最佳答案

我认为外角是由 CGContextStrokeRect(context,guideCoords); 绘制的。那时你还没有设置剪切路径,你的线宽是 10 磅,那么为什么外角会变圆呢?我认为如果在 guideCoords 矩形上调用 stroke rect 之前设置剪切路径(可能与底部的剪切路径可能不完全相同),你的运气会更好。

关于iphone - 绘制矩形中的不正确边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15665889/

相关文章:

ios - 自定义 UITableview 单元格中的链接识别

ios - drawRect 方法不绘制

ios - 如何从音频 ViewController IOS7 返回后停止音频

ios - NewRelicAgent 框架在 NR_timeElapsedInMilliSeconds 内崩溃

iphone - 查看 NSString drawInRect 将使用多少行的简单方法?

ios - 使用 iOS 7 测试版设备制作 iOS 6 应用程序

ios - 如何检测应用程序是否在 iOS 7 中自动更新

iphone - 如何从 UITouch 识别 Touch Up Inside 手势

iphone - UISplitViewController 与 NavigationControllers(包括示例代码)

ios - 如何在 iPhone 应用程序中使用 Superfeedr?