如何检测UIBezierPath上的触摸并沿着它移动球? [英] How do I detect a touch on a UIBezierPath and move a ball along that?

查看:119
本文介绍了如何检测UIBezierPath上的触摸并沿着它移动球?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何沿特定的UiBezierPath移动球?这是可能的吗?

How do I move a ball along a specific UiBezierPath? Is that possible?

我已经尝试了一切,包括使用

I've tried everything including doing a hit test using

-(BOOL)containsPoint:(CGPoint)point onPath:(UIBezierPath*)path inFillArea:(BOOL)inFill


b $ b

如何检测沿路径的触摸?

How do I detect a touch along a path?

推荐答案

首先...这是一个绝对的事实:

Firstly ... it is an absolute fact that:

有,苹果提供的NO方法,

从UIBezierPath
中提取点

there is, definitely, NO method, provided by Apple,
to extract points from a UIBezierPath
.

这是一个绝对的事实,截至2011年2月,在最新的操作系统,一切都在地平线上。我已经和苹果公司的相关工程师谈过这个问题。它在许多游戏编程语境烦人,但这是事实。

That is an absolute fact as of February 2011, in the latest OS, and everything just on the horizon. I've spoken to the relevant engineers at Apple about the issue. It's annoying in many game programming contexts, but that's the fact. So, it could be that answers your question?

其次:不要忘记它像动画一样容易使沿一个UIBezierPath的动画。对SO有许多答案,例如如何对沿弯曲路径的视图或图片的移动进行动画处理?

Secondly: don't forget it's as easy as pie to animate something along a UIBezierPath. There are numerous answers on SO, for example How can I animate the movement of a view or image along a curved path?

第三:关于发现触动发生了什么,如果这是你要的,正如Cory所说,这是一个难题!但您可以使用 CGContextPathContainsPoint 来确定点(或触摸) 是否在 指定厚度的路径。

Thirdly: Regarding finding out where touches happened, if that's what you're asking, as Cory said, this is a hard problem! But you can use CGContextPathContainsPoint to find out if a point (or touch) was on a path of a given thickness.

之后,假设我们正在谈论立方体,你需要沿着贝塞尔曲线找到点和可能的速度(又名切线)。

After that, assuming we are talking about cubics, you need to find points and likely velocities (a.k.a. tangents) along a bezier.

这是确切的,完整的代码: 查找点的切线在立方贝塞尔曲线上(在iPhone上)。我也在下面粘贴它。

Here is the exact, total code to do that: Find the tangent of a point on a cubic bezier curve (on an iPhone).. I pasted it in below, too.

你必须实现你自己的MCsim或迭代,找到它的命中,根据你的情况。这不是那么难。

You will have to implement your own MCsim or iteration to find where it hit, depending on your situation. That's not so hard.

(第四 - 作为脚注,有一个新的东西,你可以逐步绘制一个bezpath,可能与你的问题不相关,但只是一个note )

(Fourthly -- as a footnote, there's that new thing where you can progressively draw a bezpath, probably not relevant to your question but just a note.)

为了方便将来阅读的人,这里是从两个方便的例程...

For the convenience of anyone reading in the future, here is the summary from the linked question of the two "handy" routines...

最后,这里以最简单的方式是计算等距点(实际上大致等距离)沿着贝塞尔立方体:

CGFloat bezierPoint(CGFloat t, CGFloat a, CGFloat b, CGFloat c, CGFloat d)
    {
    CGFloat C1 = ( d - (3.0 * c) + (3.0 * b) - a );
    CGFloat C2 = ( (3.0 * c) - (6.0 * b) + (3.0 * a) );
    CGFloat C3 = ( (3.0 * b) - (3.0 * a) );
    CGFloat C4 = ( a );

    return ( C1*t*t*t + C2*t*t + C3*t + C4  );
    }

CGFloat bezierTangent(CGFloat t, CGFloat a, CGFloat b, CGFloat c, CGFloat d)
    {
    CGFloat C1 = ( d - (3.0 * c) + (3.0 * b) - a );
    CGFloat C2 = ( (3.0 * c) - (6.0 * b) + (3.0 * a) );
    CGFloat C3 = ( (3.0 * b) - (3.0 * a) );
    CGFloat C4 = ( a );

    return ( ( 3.0 * C1 * t* t ) + ( 2.0 * C2 * t ) + C3 );
    }

这四个预先计算的值C1 C2 C3 C4有时被称为贝塞尔的系数。 (记住a b c d通常被称为四个控制点。)当然,t从0到1,可能例如每0.05。 只需将这些例程分别调用一次X和一次Y。
希望它帮助某人!

The four precalculated values, C1 C2 C3 C4, are sometimes called the coefficients of the bezier. (Recall that a b c d are usually called the four control points.) Of course, t runs from 0 to 1, perhaps for example every 0.05. Simply call these routines once for X and separately once for Y. Hope it helps someone!

这篇关于如何检测UIBezierPath上的触摸并沿着它移动球?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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