【发布时间】:2017-11-26 17:12:07
【问题描述】:
我的目标是在我的用户在屏幕上滑动时实现类似 Adobe Illustrator 的线条。目前情况非常不稳定-这是当前情况的屏幕截图:
我认为我想要什么很明显。我希望那些直线被平滑。这叫做抗锯齿吗?
- (void)touchesBegan: (CGPoint)point
{
currentStroke = [[Stroke alloc] initWithStartPoint:point];
[strokes addObject:currentStroke];
}
- (void)touchesMoved: (CGPoint)point
{
if (!currentStroke) {
[self touchesBegan:point];
}
[currentStroke.points addObject:[NSValue valueWithCGPoint:point]]
pathTwo = [self createPath];
[self scaleToFit];
[self setNeedsDisplay];
}
- (UIBezierPath *)createPath {
UIBezierPath * bezierPath = [UIBezierPath bezierPath];
for (Stroke * stroke in strokes) {
[bezierPath moveToPoint:stroke.startPoint];
for (NSValue * value in stroke.points) {
[bezierPath addLineToPoint:[value CGPointValue]];
}
}
return bezierPath;
}
- (void)touchesEnded: (CGPoint)point
{
[points removeAllObjects];
[self setNeedsDisplay];
}
drawRect 里面有这个:
[[UIColor whiteColor] setStroke];
[pathTwo setLineWidth: _strokeWidth];
[pathTwo stroke];
编辑
我想我找到了答案…… https://github.com/jnfisher/ios-curve-interpolation
鉴于我的createPath 公式,我只是不知道如何使用它。
也许有人可以帮我插入我的数组?
【问题讨论】:
-
不,这不是抗锯齿问题。您需要访问构成整条线的各个点并对它们应用平滑操作,这将类似于 1)获取起点和终点,然后将其替换为中间的直线或 2)平均多个段的斜率等。有很多方法可以平滑曲线。
-
你能给我看一下选项#2的代码吗?
-
您的数据是如何存储的?您是否有权访问点 (x,y) 值数组?我可以提供帮助,但重要的是您开始使用什么。换句话说,每个点是否也有“时间”或一些指示,使其相对于线以正确的顺序排列?你打算如何重绘它?你需要先确定它会起作用。
-
我将数据存储在 NSMutableArray 中,首先是“stroke”(touchBegan) 以及它与 touchEnd 之间的所有关联点击...。
-
我在你的帖子中看到了 Bezier 这个词,这实际上是一个很好的算法来平滑事物。您也许可以做一些事情,例如每 5 或 10 个点取一个,并将它们作为贝塞尔曲线的控制点,而忽略其余的点。你想谷歌的是:“曲线拟合线点到平滑线”,或“贝塞尔曲线拟合点”。计算机科学/数学的艺术术语是“曲线拟合”
标签: ios objective-c uibezierpath