//绘制虚线
-(void)set{ UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 100, 320, 20)]; [self.view addSubview:imageView]; //创建一个基于位图的上下文,大小为 imageView 的大小
UIGraphicsBeginImageContext(imageView.frame.size); //绘图位置,相对画布顶点而言
[imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)]; //设置端点的格式
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapSquare);//绘制方形端点 //{10,10} 表示先绘制 10 个点,再跳过 10 个点,重复。。。 //{10,20,10} 表示先绘制 10 个,跳过 20 个,绘制 10 个点,跳过 10 个,绘制 20 个,重复。。相应的 count 要等于lengths 的个数
CGFloat lengths[] = {10,10}; CGContextRef line = UIGraphicsGetCurrentContext(); //虚线的颜色
CGContextSetStrokeColorWithColor(line, [UIColor redColor].CGColor); //phase 表示绘制的时候跳过多少个点
CGContextSetLineDash(line, 0, lengths, 2); //绘制的起点
CGContextMoveToPoint(line, 0.0, 20); //终点
CGContextAddLineToPoint(line, 310, 20); //画线
CGContextStrokePath(line); imageView.image = UIGraphicsGetImageFromCurrentImageContext(); }