如何创建具有对称角的虚线

我正在从UIBezierPath创建一个虚线,如下所示:

虚线

这是我用来构造和绘制路径的代码:

 const CGContextRef context = UIGraphicsGetCurrentContext(); const CGSize visibleSize = CGSizeMake(self.width - (kIndent * 2), self.height - (kIndent * 2)); UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(kIndent, kIndent, visibleSize.width, visibleSize.height) cornerRadius:kCornerRadius]; CGContextSetStrokeColorWithColor(context, [UIColor colorWithWhite:1.0 alpha:0.5].CGColor); [borderPath setLineWidth:2.0]; [borderPath setLineCapStyle:kCGLineCapRound]; const CGFloat pattern[] = {6, 6}; [borderPath setLineDash:pattern count:2 phase:0]; [borderPath stroke]; 

我想要的是我的广场的每个角落都具有完全相同的曲率。 我知道这最终将取决于我的模式值。 我尝试从形状的宽度计算不同的值(宽度随屏幕宽度而变化),但我无法得到我想要的外观。

有没有人之前做过这个,或有任何想法分享我怎么做?

这就是我之前所做的。 不完美,但我认为这可以帮到你。

  - (void)tailorRect:(CGRect)aRect dotsWidth:(CGFloat)dotWidth radius:(CGFloat)radius withColor:(UIColor*)color{ CGContextRef c = UIGraphicsGetCurrentContext(); CGContextSaveGState(c); CGFloat minSpacing = 10; CGFloat mWidth = aRect.size.width - radius *2; CGFloat mHeight = aRect.size.height - radius *2; int countW = mWidth / (dotWidth + minSpacing); int countH = mHeight / (dotWidth + minSpacing); CGFloat spacingW = (mWidth - (dotWidth * countW)) / (countW + 1); CGFloat spacingH = (mHeight -(dotWidth * countH)) / (countH + 1); CGContextSetLineCap(c, kCGLineCapRound); CGContextSetLineWidth(c, 1.2); CGRect tempRect = aRect; for (int r = 0; r<=1; r++) { if (r==0) { CGContextSetStrokeColorWithColor(c, [[UIColor whiteColor] colorWithAlphaComponent:0.9].CGColor); aRect = CGRectOffset(tempRect, 0, 0.5); } else { CGContextSetStrokeColorWithColor(c, color.CGColor); aRect = tempRect; } for (int w = 0; w<=1; w++) { CGFloat y = (w==0) ? aRect.origin.y : CGRectGetMaxY(aRect); CGPoint pointsW[countW*2]; for (int i = 0; i 

这很简单:

 [yourView.layer setBorderWidth:5.0]; [yourView.layer setBorderColor:[[UIColor colorWithPatternImage:[UIImage imageNamed:@"DotedImage.png"]] CGColor]];///just add image name and create image with dashed or doted drawing and add here 

在这里你只需要在项目中添加QuartzCore/QuartzCore.h框架并在.m文件中将其导入如下

 #import  

或尝试使用

 - (void)drawDashedBorderAroundView:(UIView *)v { //border definitions CGFloat cornerRadius = 10; CGFloat borderWidth = 2; NSInteger dashPattern1 = 8; NSInteger dashPattern2 = 8; UIColor *lineColor = [UIColor orangeColor]; //drawing CGRect frame = v.bounds; CAShapeLayer *_shapeLayer = [CAShapeLayer layer]; //creating a path CGMutablePathRef path = CGPathCreateMutable(); //drawing a border around a view CGPathMoveToPoint(path, NULL, 0, frame.size.height - cornerRadius); CGPathAddLineToPoint(path, NULL, 0, cornerRadius); CGPathAddArc(path, NULL, cornerRadius, cornerRadius, cornerRadius, M_PI, -M_PI_2, NO); CGPathAddLineToPoint(path, NULL, frame.size.width - cornerRadius, 0); CGPathAddArc(path, NULL, frame.size.width - cornerRadius, cornerRadius, cornerRadius, -M_PI_2, 0, NO); CGPathAddLineToPoint(path, NULL, frame.size.width, frame.size.height - cornerRadius); CGPathAddArc(path, NULL, frame.size.width - cornerRadius, frame.size.height - cornerRadius, cornerRadius, 0, M_PI_2, NO); CGPathAddLineToPoint(path, NULL, cornerRadius, frame.size.height); CGPathAddArc(path, NULL, cornerRadius, frame.size.height - cornerRadius, cornerRadius, M_PI_2, M_PI, NO); //path is set as the _shapeLayer object's path _shapeLayer.path = path; CGPathRelease(path); _shapeLayer.backgroundColor = [[UIColor clearColor] CGColor]; _shapeLayer.frame = frame; _shapeLayer.masksToBounds = NO; [_shapeLayer setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"]; _shapeLayer.fillColor = [[UIColor clearColor] CGColor]; _shapeLayer.strokeColor = [lineColor CGColor]; _shapeLayer.lineWidth = borderWidth; _shapeLayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:dashPattern1], [NSNumber numberWithInt:dashPattern2], nil]; _shapeLayer.lineCap = kCALineCapRound; //_shapeLayer is added as a sublayer of the view, the border is visible [v.layer addSublayer:_shapeLayer]; v.layer.cornerRadius = cornerRadius; } 

你可以参加一些学习

http://lukagabric.com/cashapelayer-example-round-corners-view-with-dashed-line-border/

https://github.com/lukagabric/LBorderView

使用CALayer绘制虚线

UIView带有虚线