【问题标题】:Making an image follow a pre defined path in iPhone?使图像遵循 iPhone 中的预定义路径?
【发布时间】:2010-08-24 17:37:25
【问题描述】:

我正在寻找某种方法让图像(球)沿着 iPhone 中的预定义路径移动。我的目的是创造一个类似于迷宫运动的球运动。我知道有一种方法可以使用 CGPath 以编程方式创建路径。但我相信很难创建复杂的路径。 有没有更好更简单的方法来从图像中创建路径(看起来或代表路径)并使图像(球)运动限制在该路径中?

提前感谢您的帮助...

【问题讨论】:

    标签: iphone uiimageview cgpath


    【解决方案1】:

    创建沿路径移动对象的动画并不难。例如,以下代码将沿着特定的贝塞尔曲线制作动画:

    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.duration = 1.0f;
    pathAnimation.calculationMode = kCAAnimationPaced;
    
    CGPoint currentPosition = viewToAnimate.layer.position;
    CGPoint endPoint = CGPointMake(currentPosition.x + 100.0f, currentPosition.y - 50.0f);
    CGMutablePathRef curvedPath = CGPathCreateMutable();
    CGPathMoveToPoint(curvedPath, NULL, currentPosition.x, currentPosition.y);
    CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, currentPosition.y, endPoint.x, currentPosition.y, endPoint.x, endPoint.y);
    pathAnimation.path = curvedPath;
    CGPathRelease(curvedPath);
    
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;
    [viewToAnimate.layer addAnimation:pathAnimation forKey:@"animateMovementUsingPath"];
    

    该代码的中心部分是定义路径的位置。在这种情况下,我从currentPosition 开始绘制,然后添加一条以endPoint 结束的曲线。这条曲线的控制点是(endPoint.x, currentPosition.y)(endPoint.x, currentPosition.y)

    以这种方式定义矢量曲线并让 Core Animation 为您处理所有补间要比您自己管理所有动画要容易得多。

    【讨论】:

      【解决方案2】:

      您是否考虑过实际的物理引擎?例如,Bullet 就非常棒。

      【讨论】:

      • 我正计划制作一款类似“迷宫”的游戏……所以我需要碰撞检测和其他一些功能。需要物理引擎吗?这可以通过目标 C 中的正常编码来实现吗?如果很难,你能给我推荐一些免费的 2D 物理引擎吗?
      • Box2D 浮现在脑海中。我自己没有使用过,但它用于例如Cocos2D,一个在 iPhone 世界中成熟的游戏引擎。
      猜你喜欢
      • 1970-01-01
      • 2013-02-26
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-24
      • 2020-10-30
      • 2012-07-10
      相关资源
      最近更新 更多