第一种
/    CGFloat leftPadding = 14;
//    UIBezierPath *path = [UIBezierPath bezierPath];
//         //draw a line
//    [path moveToPoint:CGPointMake(leftPadding, 0)]; //add yourStartPoint here
//    [path addLineToPoint:CGPointMake(FMScreenWidth - 2 * leftPadding, 0)];// add yourEndPoint here
//    [path addLineToPoint:CGPointMake(FMScreenWidth - 2 * leftPadding, 175.5)];// add yourEndPoint here
//    [path addLineToPoint:CGPointMake(leftPadding, 175.5)];// add yourEndPoint here
//    [path addLineToPoint:CGPointMake(leftPadding, 0)];// add yourEndPoint here
////    [path addLineToPoint:yourEndPoint];// add yourEndPoint here
//    [path stroke];
//
//     CGFloat dashPattern[] = {2.0f,6.0f,4.0f,2.0f}; //make your pattern here
//     [path setLineDash:dashPattern count:4 phase:3];
//        CAShapeLayer *shapelayer = [CAShapeLayer new];
////        [self.layer addSublayer:shapelayer];
//
//     UIColor *fill = [UIColor blueColor];
//     shapelayer.strokeStart = 0.0;
//     shapelayer.strokeColor = fill.CGColor;
//     shapelayer.lineWidth = 5.0;
//     shapelayer.lineJoin = kCALineJoinMiter;
//     shapelayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:7], nil];
//     shapelayer.lineDashPhase = 3.0f;
//     shapelayer.path = path.CGPath;


2第二种

//- (void)drawRect:(CGRect)rect {

//    CGFloat leftPadding = 14;
//
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    // 设置线条的样式
//    CGContextSetLineCap(context, kCGLineCapRound);
//    // 绘制线的宽度
//    CGContextSetLineWidth(context, 1.0);
//    // 线的颜色
//    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
//    // 开始绘制
//    CGContextBeginPath(context);
//    // 设置虚线绘制起点
//    CGContextMoveToPoint(context, leftPadding, 0);
//    // lengths的值{5,5}表示先绘制10个点,再跳过10个点,如此反复
//    CGFloat lengths[] = {5,5};
//    // 虚线的起始点
//    CGContextSetLineDash(context, 0, lengths, 2);
//    // 绘制虚线的终点
//    CGContextAddLineToPoint(context, FMScreenWidth - 2 * leftPadding, 0);
//    CGContextAddLineToPoint(context, FMScreenWidth - 2 * leftPadding, 174.5);
//    CGContextAddLineToPoint(context, leftPadding, 174.5);
//    CGContextAddLineToPoint(context, leftPadding, 0);
//    // 绘制
//    CGContextStrokePath(context);
//    // 关闭图像
//    CGContextClosePath(context);
//}


第三种
-(void)updateLine{
    CGFloat leftPadding = 14;
    if ([[[self layer] sublayers] objectAtIndex:0])
    {
        self.layer.sublayers = nil;
    }

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:self.bounds];
    [shapeLayer setPosition:self.center];
    [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
    [shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]];
    [shapeLayer setLineWidth:0.5];
    [shapeLayer setLineJoin:kCALineJoinRound];
    [shapeLayer setLineDashPattern:
    [NSArray arrayWithObjects:[NSNumber numberWithInt:5],
    [NSNumber numberWithInt:5],nil]];

    // Setup the path
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, leftPadding, 0);
    CGPathAddLineToPoint(path, NULL, FMScreenWidth - 2 * leftPadding, 0);
    CGPathAddLineToPoint(path, NULL, FMScreenWidth - 2 * leftPadding, 175.5);
    CGPathAddLineToPoint(path, NULL, leftPadding, 175.5);
    CGPathAddLineToPoint(path, NULL, leftPadding, 0);

    [shapeLayer setPath:path];
    CGPathRelease(path);

    [[self layer] addSublayer:shapeLayer];
}

/    CGFloat leftPadding \= 14;

//    UIBezierPath *path \= [UIBezierPath bezierPath];

//         //draw a line

//    [path moveToPoint:CGPointMake(leftPadding, 0)]; //add yourStartPoint here

//    [path addLineToPoint:CGPointMake(FMScreenWidth - 2 * leftPadding, 0)];// add yourEndPoint here

//    [path addLineToPoint:CGPointMake(FMScreenWidth - 2 * leftPadding, 175.5)];// add yourEndPoint here

//    [path addLineToPoint:CGPointMake(leftPadding, 175.5)];// add yourEndPoint here

//    [path addLineToPoint:CGPointMake(leftPadding, 0)];// add yourEndPoint here

////    [path addLineToPoint:yourEndPoint];// add yourEndPoint here

//    [path stroke];

//

//     CGFloat dashPattern[] \= {2.0f,6.0f,4.0f,2.0f}; //make your pattern here

//     [path setLineDash:dashPattern count:4 phase:3];

//        CAShapeLayer *shapelayer \= [CAShapeLayer new];

////        [self.layer addSublayer:shapelayer];

//

//     UIColor *fill \= [UIColor blueColor];

//     shapelayer.strokeStart \= 0.0;

//     shapelayer.strokeColor \= fill.CGColor;

//     shapelayer.lineWidth \= 5.0;

//     shapelayer.lineJoin \= kCALineJoinMiter;

//     shapelayer.lineDashPattern \= [NSArray arrayWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:7], nil];

//     shapelayer.lineDashPhase \= 3.0f;

//     shapelayer.path \= path.CGPath;


0 Comments latest

No comments.