代码之家  ›  专栏  ›  技术社区  ›  Kaspa

mkmapview的圆角

  •  8
  • Kaspa  · 技术社区  · 15 年前

    我正在构建一个自定义的UITableView,其中每个单元格包含一段文本和一个mkmapView。我希望单元格中的地图“图标”视图有圆角,这似乎是一个问题。

    我正在为我的UITableViewCell和我的 MapIcon (自定义地图视图)添加到我的UITableViewCell。

    地图图标 是的子类 MKMapView 绘制方法如下:

    -(无效)drawrect:(cgrect)rect{

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, strokeWidth);
    CGContextSetStrokeColorWithColor(context,self.strokeColor.CGColor);
    CGContextSetFillColorWithColor(context, self.rectColor.CGColor);
    
    CGFloat radius = arcRadius;
    
    CGFloat Xmin = CGRectGetMinX(rect);
    CGFloat Xmid = CGRectGetMidX(rect);
    CGFloat Xmax = CGRectGetMaxX(rect);
    
    CGFloat Ymin = CGRectGetMinY(rect);
    CGFloat Ymid = CGRectGetMidY(rect);
    CGFloat Ymax = CGRectGetMaxY(rect);   
    

    cContextBeginPath(上下文); cContextMoveTopoint(上下文,xmin,ymid); cContextAddArcTopoint(context、xmin、ymin、xmid、ymin、radius); cContextAddArcTopoint(上下文、xmax、ymin、xmax、ymid、radius); cContextAddArcTopoint(context、xmax、ymax、xmid、ymax、radius); cContextAddArcTopoint(context、xmin、ymax、xmin、ymid、radius); cContextClosePath(上下文);

    CGContextDrawPath(context, kCGPathFillStroke);
    

    cContextClip(上下文); CGContextendTransparencyLayer(上下文); }

    而且地图上的角落也没有经过处理,如下面的截图所示:

    alt text http://img190.imageshack.us/img190/948/picture1vmk.png

    如果我改变了 地图图标 要从uiview子类并使用相同的自定义绘图方法,视图将被完全剪裁,如下图所示:

    alt text http://img503.imageshack.us/img503/6269/picture2xkq.png

    以这种方式对mkmapview进行子类化并期望它被裁剪是错误的吗? 还有其他圆角的吗?

    干杯, 卡斯帕

    4 回复  |  直到 11 年前
        1
  •  40
  •   Leo Cassarani    14 年前

    最简单的圆角方法:

    #import <QuartzCore/QuartzCore.h>
    myMapView.layer.cornerRadius = 10.0;
    
        2
  •  3
  •   sara    14 年前

    只是一个小小的更正,因为在DigDog的答案中,import语句拼写错误。

    应该是

    #import <QuartzCore/QuartzCore.h>
    myMapView.layer.cornerRadius = 10.0;
    
        3
  •  1
  •   Ed Marty    15 年前

    查看概述部分的最后一段 MKMapView class reference :

    尽管您不应该将mkmapview类本身划分为子类,…

    我认为这很好地回答了您的问题,即您是否应该将其子类化。您可以做的一件事是将另一个视图放在mkmapview的顶部,该视图看起来像是圆角的背景。如果您需要它的任意大小,您可以尝试 strechableImage 方法对 UIImage .

        4
  •  0
  •   papahabla    11 年前

    如果需要的话,很容易绕过特定的拐角。例如,当地图位于 n稳定视图单元 在一个分组中 非稳定视图

    略改自 ACEToolKit :

    #import <QuartzCore/QuartzCore.h>
    - (void)awakeFromNib {
    CGRect rect = self.bounds;
    float radius = 10.f;
    
    // Create the path
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect
                                                   byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
                                                         cornerRadii:CGSizeMake(radius, radius)];
    
    // Create the shape layer and set its path
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = rect;
    maskLayer.path = maskPath.CGPath;
    
    // Set the newly created shape layer as the mask for the view's layer
    self.mapView.layer.mask = maskLayer;
    }