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

MKMapView的圆角

  •  9
  • Kaspa  · 技术社区  · 17 年前

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

    MapIcon

    地图图标 是的一个子类 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);   
    

    CGContextBeginPath(上下文); CGContextMoveToPoint(上下文,Xmin,Ymid); CGContextAddArcToPoint(上下文、Xmax、Ymax、Xmid、Ymax和半径); CGContextClosePath(上下文);

    CGContextDrawPath(context, kCGPathFillStroke);
    

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

    地图图标 从UIView子类化并使用相同的自定义绘图方法,视图将被完美剪裁,如下图所示:

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

    这些角落还有其他圆角吗?

    干杯,

    4 回复  |  直到 17 年前
        1
  •  40
  •   Leo Cassarani    15 年前

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

    应

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

    请查看《概述》一节的最后一段 MKMapView class reference :

    虽然您不应该子类化MKMapView类本身,。..

    strechableImage 方法on UIImage .

        4
  •  0
  •   papahabla    13 年前

    如果需要,也可以很容易地绕过特定的拐角。例如,当地图位于 在一个分组内 NSTableView

    略微修改自 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;
    }