Swift 绘制简单的多边形

不熟悉CGContextAddArcToPoint()用法的同学可以先看下这个例子

  CGContextRef context=UIGraphicsGetCurrentContext();
        
        CGContextSetRGBStrokeColor(context,1,0,0,1);
        CGContextMoveToPoint(context,150,50);
        CGContextAddLineToPoint(context,100,80);
        CGContextAddLineToPoint(context,130,150);
        
        CGContextMoveToPoint(context,150,50);//圆弧的起始点
        CGContextAddArcToPoint(context,100,80,130,150,50);
        CGContextStrokePath(context);

Swift 绘制简单的多边形_第1张图片
20130926134549046.png

绘制一个地图的AnnotationView的边框

let kArrorHeigh: CGFloat = 10.0

class CustomCallOutView: UIView {

 override func drawRect(rect: CGRect) {
     drawInContext(UIGraphicsGetCurrentContext())
     layer.shadowColor = UIColor.blackColor().CGColor
     layer.shadowOpacity = 1.0
     layer.shadowOffset = CGSize(width: 0, height: 0)
 }
 
 func drawInContext(context: CGContextRef) {
     CGContextSetLineWidth(context, 2.0)
     CGContextSetFillColorWithColor(context, UIColor.orangeColor().CGColor)
     drawPath(context)
     CGContextFillPath(context)
 }
 
 func drawPath(context: CGContextRef) {
     var radius:CGFloat = 6.0
     var minX    = CGRectGetMinX(bounds)
     var maxX    = CGRectGetMaxX(bounds)
     var midX    = CGRectGetMidX(bounds)
     
     var minY    = CGRectGetMinY(bounds)
     var maxY    = CGRectGetMaxY(bounds) - kArrorHeigh
     
     //开始绘制起始点 1
     CGContextMoveToPoint(context, midX + kArrorHeigh, maxY) 
     //点2 和 点1 之间绘制成一条直线
     CGContextAddLineToPoint(context, midX, maxY + kArrorHeigh)
     //点2 和 点1 之间绘制成一条直线
     CGContextAddLineToPoint(context, midX - kArrorHeigh, maxY)

     //点3, 点4 和 点 5 之间绘制一条带弧度的直角线 
     CGContextAddArcToPoint(context, minX, maxY, minX, minX, radius)
  
     CGContextAddArcToPoint(context, minX, minX, maxX, minY, radius)
     CGContextAddArcToPoint(context, maxX, minY, maxX, maxY, radius)
     CGContextAddArcToPoint(context, maxX, maxY, minX, maxY, radius)
     
     CGContextClosePath(context)    
}

你可能感兴趣的:(Swift 绘制简单的多边形)