iphone – 使用CALayer绘制虚线

iphone – 使用CALayer绘制虚线,第1张

概述我使用以下代码绘制一个虚线框: CAShapeLayer *shapeLayer = [CAShapeLayer layer];CGRect shapeRect = CGRectMake(0.0f, 0.0f, 200.0f, 100.0f);[shapeLayer setBounds:shapeRect];[shapeLayer setPosition:CGPointMake(self.c 我使用以下代码绘制一个虚线框:

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 pathCGMutablePathref path = CGPathCreateMutable();CGPathMovetoPoint(path,NulL,10,10);CGPathAddlinetoPoint(path,100,100);[shapeLayer setPath:path];CGPathRelease(path);[[self layer] addSublayer:shapeLayer];
总结

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1086564.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存