HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – 使用CALayer绘制虚线大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用以下代码绘制一个虚线框:

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
CGRect shapeRect = CGRectMake(0.0f,0.0f,200.0f,100.0f);
[shapeLayer setBounds:shapeRect];
[shapeLayer setPosition:CGPointMake(self.coreImageView_.frameX,self.coreImageView_.frameBottom - self.coreImageView_.frameHeight/2)];
[shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
[shapeLayer setstrokeColor:[[UIColor whiteColor] CGColor]];
[shapeLayer setLineWidth:2.0f];
[shapeLayer setLineJoin:kCALineJoinRound];
[shapeLayer setLineDashPattern:
[NSArray arrayWithObjects:[NSnumber numberWithInt:5],[NSnumber numberWithInt:5],nil]];

现在如果我想从点X到点B绘制一条虚线,那我应该如何修改这个代码

解决方法

通过首先将路径移动到线的起始点,然后将线段添加到点来绘制线条:

CGContextBeginPath(context);
CGContextMoveToPoint(context,10.5f,10.5f);
CGContextAddLineToPoint(context,20.5f,20.5f);
CGContextClosePath(context);
CGContextDrawPath(context,kCGPathFillstroke);

对于绘制虚线,您需要使用CAShapeLayer

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setBounds:self.bounds];
[shapeLayer setPosition:self.center];
[shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
[shapeLayer setstrokeColor:[[UIColor blackColor] CGColor]];
[shapeLayer setLineWidth:3.0f];
[shapeLayer setLineJoin:kCALineJoinRound];
[shapeLayer setLineDashPattern:
 [NSArray arrayWithObjects:[NSnumber numberWithInt:10],nil]];

// Setup the path
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path,NULL,10,10);
CGPathAddLineToPoint(path,100,100);

[shapeLayer setPath:path];
CGPathRelease(path);

[[self layer] addSublayer:shapeLayer];

大佬总结

以上是大佬教程为你收集整理的iphone – 使用CALayer绘制虚线全部内容,希望文章能够帮你解决iphone – 使用CALayer绘制虚线所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。