1

我正在尝试平滑我的手绘贝塞尔曲线,但无法实现,我从 erica sudan 写的书中获得了平滑曲线的代码,我试图实现,但不知道为什么我不能产生一个平滑的曲线,下面是我的代码,我请求大家请通过它..

- (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;
}
4

1 回答 1

0

我建议观看 WWDC 2012 的“构建高级手势识别器”。您可以跳过开头 - 您最感兴趣的部分在 38:23。

于 2012-07-10T13:51:11.273 回答