開發中經常用到虛線
創建一個imageView,直接調用下面的代碼就可以了!,imageView的高一般設置2像素就可以了
- (void)drawLineByImageView:(UIImageView *)imageView { UIGraphicsBeginImageContext(imageView.frame.size); //開始畫線 划線的frame [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)]; //設置線條終點形狀 CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextRef line = UIGraphicsGetCurrentContext(); // 設置顏色 CGContextSetStrokeColorWithColor(line, [UIColor darkGrayColor].CGColor); CGFloat lengths[] = {5,2};//先畫4個點再畫2個點 CGContextSetLineDash(line,0, lengths,2);//注意2(count)的值等於lengths數組的長度 CGContextMoveToPoint(line, 0.0, 2.0); //開始畫線 CGContextAddLineToPoint(line,imageView.frame.size.width,2.0);
CGContextStrokePath(line); // UIGraphicsGetImageFromCurrentImageContext()返回的就是image UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); imageView.image = image; }