iOS 繪制虛線


開發中經常用到虛線

創建一個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; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM