//此方法目前只能添加在self視圖上(如果有視圖在self視圖上,可能會遮蓋線條),畫的是虛線 //調用 [self drawRect:self.frame]; //覆寫 - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextBeginPath(context); CGContextSetLineWidth(context, 1 *kScreenScale);//線條長度 CGContextSetStrokeColorWithColor(context, [UIColor colorWithHex:@"#959595"].CGColor);//線條顏色 CGFloat lengths[] = {2*kScreenScale,1*kScreenScale};//設置虛線 CGContextSetLineDash(context, 0, lengths, 2);//設置虛線 CGContextMoveToPoint(context, 5 *kScreenScale, 3*kScreenScale);//起點 CGContextAddLineToPoint(context, 360*kScreenScale, 3*kScreenScale);//畫到第一個點 CGContextAddLineToPoint(context, 362*kScreenScale, 5*kScreenScale);//畫到第二個點 CGContextAddLineToPoint(context, 362*kScreenScale, 100*kScreenScale);//以下類推 CGContextAddLineToPoint(context, 360*kScreenScale, 101*kScreenScale); CGContextAddLineToPoint(context, 5*kScreenScale, 101*kScreenScale); CGContextAddLineToPoint(context, 4*kScreenScale, 100*kScreenScale); CGContextAddLineToPoint(context, 4*kScreenScale, 5*kScreenScale); CGContextAddLineToPoint(context, 5 *kScreenScale, 3*kScreenScale); CGContextStrokePath(context);//收尾 CGContextClosePath(context);//結束 }