iOS 学习 - 16.绘制虚线

2023-05-07,,

//绘制虚线
-(void)set{ UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];
[self.view addSubview:imageView];
//创建一个基于位图的上下文,大小为 imageView 的大小
UIGraphicsBeginImageContext(imageView.frame.size);
//绘图位置,相对画布顶点而言
[imageView.image drawInRect:CGRectMake(, , 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[] = {,};
CGContextRef line = UIGraphicsGetCurrentContext();
//虚线的颜色
CGContextSetStrokeColorWithColor(line, [UIColor redColor].CGColor);
//phase 表示绘制的时候跳过多少个点
CGContextSetLineDash(line, , lengths, );
//绘制的起点
CGContextMoveToPoint(line, 0.0, );
//终点
CGContextAddLineToPoint(line, , );
//画线
CGContextStrokePath(line); imageView.image = UIGraphicsGetImageFromCurrentImageContext();
}

iOS 学习 - 16.绘制虚线的相关教程结束。

《iOS 学习 - 16.绘制虚线.doc》

下载本文的Word格式文档,以方便收藏与打印。