如何从UIBezierPath生成CGPoint-Array(沿给定路径触摸滑动对象) [英] How to generate CGPoint-Array out of UIBezierPath (to touch-slide object along given path)
问题描述
我有一个UIBezierPath(曲线像'8',只有4个点),我需要制作一些CGPoint-Array。有任何想法吗? thanx!
I have a UIBezierPath (curved like an '8', with only 4 points in it) and I need to make some kind of CGPoint-Array out of it. Any ideas? thanx!
编辑:
我的bezier初始化就像那样
I have my bezier initlialized like that
-(void) initBezier
{
theBezierPath = [UIBezierPath bezierPath];
[theBezierPath moveToPoint:P(211.00, 31.00)];
[theBezierPath addCurveToPoint:P(870.00, 191.00) controlPoint1:P(432.00, -11.00) controlPoint2:P(593.00, 209.00)];
[theBezierPath addCurveToPoint:P(731.00, 28.00) controlPoint1:P(1061.95, 178.53) controlPoint2:P(944.69, 5.78)];
[theBezierPath addCurveToPoint:P(189.00, 190.00) controlPoint1:P(529.00, 49.00) controlPoint2:P(450.00, 189.00)];
[theBezierPath addCurveToPoint:P(211.00, 31.00) controlPoint1:P(-33.01, 190.85) controlPoint2:P(71.00, 37.00)];
}
我用
anim = [CAKeyframeAnimation animationWithKeyPath:@"emitterPosition"];
anim.path = theBezierPath.CGPath;
anim.calculationMode = kCAAnimationCubicPaced;
anim.repeatCount = HUGE_VALF;
anim.duration = tme;
我想逐个像素地(通过触摸位置)为路径上的对象设置动画。我希望对象将触摸的给定坐标捕捉到曲线上最近的点,以便沿着路径触摸滑动对象。
I want to animate the object on the path pixel by pixel (via touch-position). I want the object to "snap" a given coordinate of a touch to the nearest point on the curve so it touch-slides the object along the path.
推荐答案
使用 CGPathApply()
函数迭代路径中的所有元素。作为参数之一,您必须指定一个函数的指针,然后为每个路径元素调用该函数。路径元素数据结构(类型 CGPathElement
)然后包含描述它的点。
Use the CGPathApply()
function to iterate over all elements in a path. As one of the arguments, you have to specify a pointer to a function that will then be called for each path element. The path element data structure (of type CGPathElement
) then contains the point(s) that describe it.
使用 NSValue
作为将点添加到 NSMutableArray
的包装器。
Use NSValue
as a wrapper to add the points to an NSMutableArray
.
这篇关于如何从UIBezierPath生成CGPoint-Array(沿给定路径触摸滑动对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!