【问题标题】:CAShapeLayer Dashed Lines While Drawing绘制时 CAShapeLayer 虚线
【发布时间】:2012-08-28 21:52:47
【问题描述】:

我正在向应用程序添加一个简单的绘图功能,并且我希望用户能够绘制虚线。我的绘图程序工作得很好,添加了一个 CAShapeLayer 并设置了路径。

但是,当我尝试绘制虚线时,只有当用户绘制速度非常快时,虚线才明显。如果我慢慢画,线条是实心的,因为破折号聚集在一起。

步骤 1. 开始一个新的绘图层。一些代码显然被遗漏了。

// CAShapeLayer
// Dashed pattern is arbitrary - I've tried a variety of values
[self.currentDrawLayer setLineDashPattern:[NSArray arrayWithObjects:
                                          [NSNumber numberWithInt:6],
                                          [NSNumber numberWithInt:12],
                                          nil]];

currentPenPath = [UIBezierPath bezierPath];    
[currentPenPath moveToPoint:currentPoint];

第 2 步。在用户绘制和更新图层时添加点。

[currentPenPath addLineToPoint:currentPoint];
[currentPenPath moveToPoint:currentPoint];
[[self currentDrawLayer] setPath:currentPenPath.CGPath];

我希望用户能够随心所欲地画得慢或快,并且在绘制时有一致的破折号。我需要先把所有的点都弄平吗?

有没有一种简单的方法可以在用户绘制时绘制破折号?

感谢任何指导。

【问题讨论】:

  • 听起来你需要对路径进行后处理并移除点。
  • 我一直在想。我收集我需要以某种方式创建一条遵循相同曲线但点均匀分布的新路径。我不知道该怎么做,所以我正在研究。

标签: iphone cashapelayer


【解决方案1】:

我找到了一个解决方案 - 并且在绘图时也可以使用!

一些代码...

else if (sender.state == UIGestureRecognizerStateChanged)
{
    CGFloat distance = sqrtf(powf((point.x - currentPenPoint.x), 2) + powf((point.y - currentPenPoint.y), 2));

    if (distance > 20) // TODO: define magic number after some testing
    {
        currentPenPoint = point;
        [self drawPenLine];
    }

    <snip>
}

我参考了以下帖子: How to draw Smooth straight line by using UIBezierPath?

使用毕达哥拉斯我可以找到前一点之间的距离,如果足够远,我添加点并画线。这样虚线的间距是均匀的。

【讨论】:

  • 跳过sqrtf(),直接调整距离阈值。行为相同,但效率更高。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-21
  • 2017-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多