Ios 平滑手绘贝塞尔曲线
我正在尝试平滑我的手绘贝塞尔曲线,但无法实现,它,我从erica Sundan编写的书中获得了平滑曲线的代码,我试图实现,但不知道为什么我无法生成平滑曲线,下面是我的代码,我请大家仔细阅读Ios 平滑手绘贝塞尔曲线,ios,cocoa-touch,core-graphics,bezier,Ios,Cocoa Touch,Core Graphics,Bezier,我正在尝试平滑我的手绘贝塞尔曲线,但无法实现,它,我从erica Sundan编写的书中获得了平滑曲线的代码,我试图实现,但不知道为什么我无法生成平滑曲线,下面是我的代码,我请大家仔细阅读 - (void)drawRect:(CGRect)rect { myPath.pathColor = [UIColor redColor]; for (BezeirPath *_path in m_pathArray) { [_path.
- (void)drawRect:(CGRect)rect
{
myPath.pathColor = [UIColor redColor];
for (BezeirPath *_path in m_pathArray)
{
[_path.pathColor setStroke];
[_path.bezeirPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
m_previousPoint1 = [mytouch previousLocationInView:self];
m_previousPoint2 = [mytouch previousLocationInView:self];
m_currentPoint = [mytouch locationInView:self];
CGPoint controlPoint = CGPointMake(m_previousPoint1.x+(m_previousPoint1.x - m_previousPoint2.x), m_previousPoint1.y +(m_previousPoint1.y - m_previousPoint2.y));
[myPath.bezeirPath addQuadCurveToPoint:m_currentPoint controlPoint:controlPoint];
self.previousPoint3 = controlPoint;
[myPath.bezeirPath smoothedPath:myPath.bezeirPath :40];//Called the smoothing function.
[self setNeedsDisplay];
}
//平滑函数
-(UIBezierPath*)smoothedPath:(UIBezierPath*)bpath: (NSInteger) granularity
{
NSArray *points = pointsFromBezierPath(bpath);
if (points.count < 4) return bpath;
// This only copies lineWidth. You may want to copy more
UIBezierPath *smoothedPath = [UIBezierPath bezierPath];
smoothedPath.lineWidth = bpath.lineWidth;
// Draw out the first 3 points (0..2)
[smoothedPath moveToPoint:POINT(0)];
for (int index = 1; index < 3; index++)
[smoothedPath addLineToPoint:POINT(index)];
// Points are interpolated between p1 and p2,
// starting with 2..3, and moving from there
for (int index = 4; index < points.count; index++)
{
CGPoint p0 = POINT(index - 3);
CGPoint p1 = POINT(index - 2);
CGPoint p2 = POINT(index - 1);
CGPoint p3 = POINT(index);
// now add n points starting at p1 + dx/dy up until p2
// using Catmull-Rom splines
for (int i = 1; i < granularity; i++)
{
float t = (float) i * (1.0f / (float) granularity);
float tt = t * t;
float ttt = tt * t;
CGPoint pi; // intermediate point
pi.x = 0.5 * (2*p1.x+(p2.x-p0.x)*t +
(2*p0.x-5*p1.x+4*p2.x-p3.x)*tt +
(3*p1.x-p0.x-3*p2.x+p3.x)*ttt);
pi.y = 0.5 * (2*p1.y+(p2.y-p0.y)*t +
(2*p0.y-5*p1.y+4*p2.y-p3.y)*tt +
(3*p1.y-p0.y-3*p2.y+p3.y)*ttt);
[smoothedPath addLineToPoint:pi];
}
[smoothedPath addLineToPoint:p2];
}
// finish by adding the last point
[smoothedPath addLineToPoint:POINT(points.count - 1)];
return smoothedPath;
}
-(UIBezierPath*)平滑路径:(UIBezierPath*)bpath:(NSInteger)粒度
{
NSArray*points=点从BezierPath(bpath)开始;
如果(points.count<4)返回bpath;
//这仅复制线宽。您可能需要复制更多
UIBezierPath*平滑路径=[UIBezierPath bezierPath];
平滑路径.lineWidth=bpath.lineWidth;
//画出前3个点(0..2)
[平滑路径移动点:点(0)];
对于(int-index=1;index<3;index++)
[平滑路径addLineToPoint:点(索引)];
//点在p1和p2之间插值,
//从2..3开始,然后从那里开始
对于(int index=4;index
我建议观看WWDC 2012中的“构建高级手势识别器”。您可以跳过开头-您最感兴趣的部分是38:23。不,我必须将bezeirPath对象传递给此函数。如果我错了,请纠正我。你好,朋友,有人能在这方面建议我吗?谢谢@Lenny K的回复,因为性能问题,我跳过了使用BezeirPath的方法?你对我的决定有什么看法?请回答您决定使用什么替代?我正在使用CgContex及其相关函数和Cglayer来缓存drawingOK。不幸的是,我对这个话题知道的不多,所以我不能给你一个好的建议。不,不幸的是,关于这个话题,我唯一知道的是我上面提到的视频。