Jam*_*son 7 iphone objective-c cgcontext ios
我正在尝试画一条虚线CGContextSetLineDash.
这是我的代码:
float dashPhase = 0.0;
float dashLengths[] = {30, 30};
CGContextSetLineDash(context, dashPhase, dashLengths, 20.0);
self.previousPoint2 = self.previousPoint1;
self.previousPoint1 = previous;
self.currentPoint = current;
self.mid1 = [self pointBetween:self.previousPoint1 andPoint:self.previousPoint2];
self.mid2 = [self pointBetween:self.currentPoint andPoint:self.previousPoint1];
UIBezierPath* newPath = [UIBezierPath bezierPath];
[newPath moveToPoint:self.mid1];
[newPath addLineToPoint:self.mid2];
[newPath setLineWidth:self.brushSize];
Run Code Online (Sandbox Code Playgroud)
但是,如果我画得很慢,它们的虚线就不会出现(见下图的顶部),但如果我快速绘制,它们会出现(见下图底部).

为什么会这样?
您已设置dashPhase = 0.,因此每次开始新行时,图案都会以 30 单位的已绘制线段开始,然后是 30 单位的未绘制线段。如果线段很短,则将绘制整条线。
因此,要么使用单个路径(仅在其中附加线段),要么为每个新子路径计算从dashPhase何处开始模式。
(最后一个参数不应该CGContextSetLineDash是 的长度dashLengths[]吗2?)
更新:正如我们在讨论中发现的,问题的解决方案确实是在最后一个贝塞尔路径中添加线段,只要用户绘制相同的曲线即可:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// ...
// Compute nextPoint to draw ...
UIBezierPath *lastPath = [self.paths lastObject];
[lastPath addLineToPoint:self.nextPoint];
// ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8891 次 |
| 最近记录: |