oc画虚线

oc画虚线

//
//  Xuxian.h
//  iaiai
//
//  Created by iaiai on 15/2/14.
//  Copyright (c) 2015年 iaiai. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface Xuxian : UIView

@property(nonatomic)CGPoint startPoint;//虚线起点

@property(nonatomic)CGPoint endPoint;//虚线终点

@property(nonatomic,strong)UIColor* lineColor;//虚线颜色

@end



//
//  Xuxian.m
//  iaiai
//
//  Created by iaiai on 15/2/14.
//  Copyright (c) 2015年 iaiai. All rights reserved.
//

#import "Xuxian.h"

@implementation Xuxian

- (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[] = {10,5};
    CGContextSetLineDash(context, 0, lengths, 2);
    CGContextStrokePath(context); //开始画线
}

@end



    Xuxian* xuxian = [[Xuxian alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
    [xuxian setBackgroundColor:[UIColor whiteColor]];
    [xuxian setStartPoint:CGPointMake(10, 0)];
    [xuxian setEndPoint:CGPointMake(310, 0)];
    [xuxian setLineColor:[self colorWithHexString:@"#bfbfbf"]];
    [scrollView addSubview:xuxian];

猜你喜欢

转载自iaiai.iteye.com/blog/2185937