CGPath在点之间添加曲线 [英] CGPath adding a curve between points

查看:112
本文介绍了CGPath在点之间添加曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个函数,它随机生成一些CGPoint,然后从这些点创建一个CGPath。我在关键帧动画中使用此路径为屏幕周围的视图设置动画。

So I have a function which randomly generates some CGPoints I then create a CGPath from these points. I use this path in a keyframe animation to animate a view around the screen.

该路径是使用以下代码生成的:

The path is generated using the code below:

- (CGMutablePathRef)generatePathFromCoordinates:(NSArray *)coordinates
{
    CGMutablePathRef path = CGPathCreateMutable();
    CGPoint point = [(NSValue *)[coordinates objectAtIndex:0] CGPointValue];
    CGPathMoveToPoint(path, nil, point.x, point.y);

    for (int i=1; i < coordinates.count; i++)
    {
        point = [(NSValue *)[coordinates objectAtIndex:i] CGPointValue];
        CGPathAddLineToPoint(path, nil, point.x, point.y);
    }

    return path;   
}

上面的代码很好用,但是动画看起来有点奇怪,所以我d想在坐标之间而不是直线上添加曲线。如下图所示(我知道惊人的绘画技巧!)。路径 A是我当前正在执行的操作,路径 B是我正在针对以下目标:

The above code works great but the animation looks a little strange, so I'd like to add a curve between coordinates rather than a straight line. Like the picture below (amazing paint skills i know!). Path 'A' is what I'm currently doing and path 'B' is what I'm aiming for:

在查看CGPath参考中的可用功能后,我可以看到一个 CGPathAddArcToPoint CGPathAddCurveToPoint 很少能完成工作。我已经简短地使用了这些功能,但是无法获得预期的效果(可能是因为我使用不正确)

After having a look at the functions available in the CGPath reference I can see a few which might get the job done, CGPathAddArcToPoint CGPathAddCurveToPoint. I've had a brief go at using these functions but wasn't able to get the desired effect (probably because I was using them incorrectly)

你们能指点我吗

预先感谢

推荐答案

CGPathAddCurveToPoint 将三次曲线添加到贝塞尔曲线路径。如果您不知道,它的制作方式是由两个控制点确定曲线的形状(对曲线使用三年级方程式)。如果要使曲线具有特定的形状,则需要计算两个中间点,这些中间点偏向于路径当前点和曲线终点。相反,如果您不关心具有特定形状的曲线,则可以选择一些随机点。这是一个实现示例:

CGPathAddCurveToPoint adds a cubic curve to the bezier path. In the case you don't know it, it's made in a way that the shape of the curve is determined by two control points (uses a third grade equation for the curve). If you want the curve to have a particular shape, then you need to calculate two intermediate points, biased on the value of the path current point, and the curve end point. If instead you don't care about having a curve with a particular shape, you can just pick some random points. This is an implementation example:

for (int i=1; i < coordinates.count; i++)
{
    point = [[coordinates objectAtIndex:i] CGPointValue];
    CGPathAddCurveToPoint(path, 0, randomX(), randomY(), randomX(), randomY(), point.x, point.y);
}

我看到您使用了 CGPath ,但是您也可以将OOP与 UIBezierPath 一起使用如果您发现它更容易使用,则可以使用 CGPath 代替。

I see the you used CGPath, but you can also use OOP with UIBezierPath instead of CGPath, if you find it easier to use.

这篇关于CGPath在点之间添加曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆