美文网首页
如何实现OC下划虚线

如何实现OC下划虚线

作者: phzean | 来源:发表于2016-01-15 12:32 被阅读299次

    封装一个画虚线的类

    .h文件
     #import <UIKit/UIKit.h>    
    @interface PHDashedLineView : UIView   
    @property(nonatomic,assign)CGPoint startPoint;//虚线起点    
    @property(nonatomic,assign)CGPoint endPoint;//虚线终点    
    @property(nonatomic,strong)UIColor* lineColor;//虚线颜色
    @property(nonatomic,assign)CGFloat solidWidth; //虚线中横线的宽度
    @property(nonatomic,assign)CGFloat spaceWidth; //虚线中空白地方的宽度
    @end
    
    .m文件
    #import "PHDashedLineView.h"
    
    @implementation PHDashedLineView
    
    - (void)drawRect:(CGRect)rect{
    CGContextRef context = UIGraphicsGetCurrentContext(); //设置上下文
    CGContextSetStrokeColorWithColor(context, _lineColor.CGColor);//线条颜色
    CGContextSetLineWidth(context, 1.0);//线条宽度
    CGContextMoveToPoint(context, _startPoint.x, _startPoint.y); //开始画线, x,y 为开始点的坐标
    CGContextAddLineToPoint(context, _endPoint.x, _endPoint.y);//画直线, x,y 为线条结束点的坐标
    CGFloat lengths[] = {_solidWidth,_spaceWidth};
    CGContextSetLineDash(context, 0, lengths, 2);
    CGContextStrokePath(context); //开始画线
    }
    
    @end
    
    使用
    PHDashedLineView *view = [[PHDashedLineView alloc] initWithFrame:CGRectMake(0, PCView_H(self) - 1, SCREEN_WIDTH, 1)];
    view.backgroundColor = [UIColor clearColor];
    view.lineColor = COLORRGB187;
    view.solidWidth = 1;
    view.spaceWidth = 5;
    view.startPoint = CGPointMake(0, 0);
    view.endPoint = CGPointMake(self.frame.size.width, 0);
    _dashedView = view;
    [self addSubview:view];
    

    上面例子中最后结果是画了一条水平的直线,可以通过开始和结束的坐标画出不同倾斜度的直线出来,如果在这行代码<code>view.endPoint = CGPointMake(self.frame.size.width, 0);</code>后面加上<code>view.endPoint = CGPointMake(self.frame.size.width, 120);</code>就会画出一条折线出来

    更多好玩的,大家可以慢慢琢磨

    网上还看到一种写法,使用<code>CAShapeLayer</code>来绘制,下面附上代码:

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 132, 150)];
    view.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:view];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setFrame:view.bounds];
    [shapeLayer setPosition:CGPointMake(0, 0)];
    [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
    // 设置虚线的颜色
    [shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]];
    // 设置虚线的高度
    [shapeLayer setLineWidth:1.0f];
    [shapeLayer setLineJoin:kCALineJoinRound];
    // 3=线的宽度 1=每条线的间距
    [shapeLayer setLineDashPattern:
     [NSArray arrayWithObjects:[NSNumber numberWithInt:3],
      [NSNumber numberWithInt:1],nil]];
    
    // Setup the path
    CGMutablePathRef path = CGPathCreateMutable();
    //给出起始和终点path
    CGFloat coordinateX = view.frame.size.width / 2;
    CGFloat coordinateY = view.frame.size.height / 2 * 3;
    CGPathMoveToPoint(path, NULL, coordinateX, coordinateY);
    CGPathAddLineToPoint(path, NULL, coordinateX + view.frame.size.width,coordinateY);
    CGPathAddLineToPoint(path, NULL, coordinateX + view.frame.size.width,coordinateY - view.frame.size.height);
    [shapeLayer setPath:path];
    
    [[view layer] addSublayer:shapeLayer];
    
    CGPathRelease(path);
    

    请戳:原文链接

    相关文章

      网友评论

          本文标题:如何实现OC下划虚线

          本文链接:https://www.haomeiwen.com/subject/tkshkttx.html