12

我正在尝试沿贝塞尔路径移动的 UIImageView 对象位置的关键帧动画。这张图片显示了动画之前的初始状态。蓝线是路径 - 最初是直线向上移动,浅绿色框是初始边界框或图像,深绿色“幽灵”是我正在移动的图像:

初始状态

当我将 rotationMode 设置为 的动画开始时nil,图像在整个路径中保持相同的方向,如预期的那样。

但是,当我将 rotationMode 设置为 的动画开始时kCAAnimationRotateAuto,图像会立即逆时针旋转 90 度,并在整个路径中一直保持这个方向。当它到达路径的末端时,它会以正确的方向重绘(它实际上显示了我在最终位置重新定位的 UIImageView)

在此处输入图像描述

我天真地期望rotationMode会将图像定向到路径的切线而不是法线,尤其是当Apple docs for the CAKeyframeAnimation rotationMode state

确定沿路径设置动画的对象是否旋转以匹配路径切线。

那么这里的解决方案是什么?我是否必须将图像顺时针预旋转 90 度?或者有什么我想念的吗?

谢谢你的帮助。


3月2日编辑

我使用仿射旋转在路径动画之前添加了一个旋转步骤,例如:

theImage.transform = CGAffineTransformRotate(theImage.transform,90.0*M_PI/180);

然后在路径动画之后,重置旋转:

theImage.transform = CGAffineTransformIdentity;

这使得图像以预期的方式跟随路径。但是,我现在遇到了图像闪烁的另一个问题。我已经在寻找这个 SO 问题中闪烁问题的解决方案:

iOS CAKeyFrameAnimation 缩放在动画结束时闪烁

所以现在我不知道我是否让事情变得更好或更糟!


编辑 3 月 12 日

虽然 Caleb 指出是的,但我确实必须预先旋转我的图像,但 Rob 提供了一个很棒的代码包,几乎完全解决了我的问题。Rob 唯一没有做的事情是补偿我的资产以垂直而不是水平方向绘制,因此在制作动画之前仍需要将它们预旋转 90 度。但是,嘿,我必须做一些工作才能让事情正常运行,这是唯一公平的。

因此,为了满足我的要求,我对 Rob 的解决方案的细微改动是:

  1. 当我添加 UIView 时,我预先旋转它以抵消通过设置添加的固有旋转rotationMode

    theImage.transform = CGAffineTransformMakeRotation(90*M_PI/180.0);

  2. 我需要在动画结束时保持该旋转,因此在定义完成块后,我不只是用新的比例因子爆破视图的变换,而是根据当前变换构建比例:

    theImage.transform = CGAffineTransformScale(theImage.transform, scaleFactor, scaleFactor);

这就是我所要做的,让我的形象按照我的预期走!


编辑 3 月 22 日

我刚刚向 GitHub 上传了一个演示项目,该项目展示了对象沿贝塞尔路径的移动。代码可以在PathMove找到

我还在我的博客“在 iOS 中沿贝塞尔路径移动对象”中写到了它

4

3 回答 3

8

这里的问题是 Core Animation 的自动旋转使视图的水平轴与路径的切线平行。这就是它的工作原理。

如果您希望视图的垂直轴跟随路径的切线,那么按照您当前的操作旋转视图的内容是合理的做法。

于 2012-03-03T15:00:22.593 回答
6

以下是消除闪烁需要了解的内容:

  • 正如 Caleb 指出的那样,Core Animation 会旋转您的图层,使其正 X 轴位于您路径的切线。您需要使您的图像的“自然”方向与之配合使用。因此,假设在您的示例图像中这是一艘绿色宇宙飞船,您需要宇宙飞船在没有应用旋转时指向右侧。

  • 设置包含旋转的变换会干扰“kCAAnimationRotateAuto”应用的旋转。在应用动画之前,您需要从变换中删除旋转。

  • 当然,这意味着您需要在动画完成时重新应用转换。当然,您希望这样做而不会在图像外观上看到任何闪烁。这并不难,但其中涉及到一些秘方,我将在下面解释。

  • 您可能希望您的宇宙飞船开始沿着路径的切线指向,即使当宇宙飞船静止不动时还没有动画。如果您的飞船图像指向右侧,但您的路径向上,那么您需要将图像的变换设置为包括 90° 旋转。但也许您不想对旋转进行硬编码 - 相反,您想查看路径并找出它的起始切线。

我将在这里展示一些重要的代码。你可以在 github 上找到我的测试项目。您可能会在下载和试用它时发现一些用处。只需点击绿色的“宇宙飞船”即可查看动画。

因此,在我的测试项目中,我已将 my 连接UIImageView到一个名为animate:. 当您触摸它时,图像会沿着数字 8 的一半移动,并且大小会翻倍。当你再次触摸它时,图像沿着数字 8 的另一半移动(回到起始位置),并恢复到原来的大小。两个动画都使用kCAAnimationRotateAuto,因此图像沿路径的切线指向。

这是 的开始animate:,我在这里确定图像应该结束的路径、比例和目标点:

- (IBAction)animate:(id)sender {
    UIImageView* theImage = self.imageView;

    UIBezierPath *path = _isReset ? _path0 : _path1;
    CGFloat newScale = 3 - _currentScale;

    CGPoint destination = [path currentPoint];

所以,我需要做的第一件事是从图像的变换中删除任何旋转,因为正如我所提到的,它会干扰kCAAnimationRotateAuto

    // Strip off the image's rotation, because it interferes with `kCAAnimationRotateAuto`.
    theImage.transform = CGAffineTransformMakeScale(_currentScale, _currentScale);

接下来,我进入一个UIView动画块,以便系统将动画应用于图像视图:

    [UIView animateWithDuration:3 animations:^{

我为该位置创建了关键帧动画并设置了它的几个属性:

        // Prepare my own keypath animation for the layer position.
        // The layer position is the same as the view center.
        CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        positionAnimation.path = path.CGPath;
        positionAnimation.rotationMode = kCAAnimationRotateAuto;

接下来是防止动画结尾闪烁的秘诀。回想一下,动画不会影响您将它们附加到的“模型层”的属性(theImage.layer在这种情况下)。相反,他们更新了“表示层”的属性,它反映了屏幕上的实际内容。

所以首先我设置removedOnCompletionNO关键帧动画。这意味着动画完成后,动画将保持连接到模型层,这意味着我可以访问表示层。我从表示层获取变换,删除动画,并将变换应用到模型层。由于这一切都发生在主线程上,所以这些属性的变化都是在一个屏幕刷新周期内发生的,所以没有闪烁。

        positionAnimation.removedOnCompletion = NO;
        [CATransaction setCompletionBlock:^{
            CGAffineTransform finalTransform = [theImage.layer.presentationLayer affineTransform];
            [theImage.layer removeAnimationForKey:positionAnimation.keyPath];
            theImage.transform = finalTransform;
        }];

现在我已经设置了完成块,我实际上可以更改视图属性。当我这样做时,系统会自动将动画附加到图层。

        // UIView will add animations for both of these changes.
        theImage.transform = CGAffineTransformMakeScale(newScale, newScale);
        theImage.center = destination;

我将自动添加的位置动画中的一些关键属性复制到我的关键帧动画中:

        // Copy properties from UIView's animation.
        CAAnimation *autoAnimation = [theImage.layer animationForKey:positionAnimation.keyPath];
        positionAnimation.duration = autoAnimation.duration;
        positionAnimation.fillMode = autoAnimation.fillMode;

最后我将自动添加的位置动画替换为关键帧动画:

        // Replace UIView's animation with my animation.
        [theImage.layer addAnimation:positionAnimation forKey:positionAnimation.keyPath];
    }];

最后,我更新了我的实例变量以反映对图像视图的更改:

    _currentScale = newScale;
    _isReset = !_isReset;
}

这就是没有闪烁的动画图像视图。

而现在,正如史蒂夫乔布斯所说,最后一件事。当我加载视图时,我需要设置图像视图的变换,以便它旋转以指向我将用来为其设置动画的第一条路径的切线。我在一个名为的方法中做到这一点reset

- (void)reset {
    self.imageView.center = _path1.currentPoint;
    self.imageView.transform = CGAffineTransformMakeRotation(startRadiansForPath(_path0));
    _currentScale = 1;
    _isReset = YES;
}

当然,棘手的部分隐藏在该startRadiansForPath函数中。这真的没那么难。我使用该CGPathApply函数来处理路径的元素,挑选出实际形成子路径的前两个点,然后计算由这两个点形成的线的角度。(曲线路径部分是二次或三次贝塞尔样条曲线,并且这些样条曲线具有样条曲线第一个点的切线是从第一个点到下一个控制点的线的属性。)

为了后代,我只是在这里转储代码而不做解释:

typedef struct {
    CGPoint p0;
    CGPoint p1;
    CGPoint firstPointOfCurrentSubpath;
    CGPoint currentPoint;
    BOOL p0p1AreSet : 1;
} PathState;

static inline void updateStateWithMoveElement(PathState *state, CGPathElement const *element) {
    state->currentPoint = element->points[0];
    state->firstPointOfCurrentSubpath = state->currentPoint;
}

static inline void updateStateWithPoints(PathState *state, CGPoint p1, CGPoint currentPoint) {
    if (!state->p0p1AreSet) {
        state->p0 = state->currentPoint;
        state->p1 = p1;
        state->p0p1AreSet = YES;
    }
    state->currentPoint = currentPoint;
}

static inline void updateStateWithPointsElement(PathState *state, CGPathElement const *element, int newCurrentPointIndex) {
    updateStateWithPoints(state, element->points[0], element->points[newCurrentPointIndex]);
}

static void updateStateWithCloseElement(PathState *state, CGPathElement const *element) {
    updateStateWithPoints(state, state->firstPointOfCurrentSubpath, state->firstPointOfCurrentSubpath);
}

static void updateState(void *info, CGPathElement const *element) {
    PathState *state = info;
    switch (element->type) {
        case kCGPathElementMoveToPoint: return updateStateWithMoveElement(state, element);
        case kCGPathElementAddLineToPoint: return updateStateWithPointsElement(state, element, 0);
        case kCGPathElementAddQuadCurveToPoint: return updateStateWithPointsElement(state, element, 1);
        case kCGPathElementAddCurveToPoint: return updateStateWithPointsElement(state, element, 2);
        case kCGPathElementCloseSubpath: return updateStateWithCloseElement(state, element);
    }
}

CGFloat startRadiansForPath(UIBezierPath *path) {
    PathState state;
    memset(&state, 0, sizeof state);
    CGPathApply(path.CGPath, &state, updateState);
    return atan2f(state.p1.y - state.p0.y, state.p1.x - state.p0.x);
}
于 2012-03-06T09:03:55.417 回答
3

你提到你用“rotationMode设置为YES”开始动画,但文档指出rotationMode应该使用NSString设置......

尤其:

这些常量由 rotationMode 属性使用。

NSString * const kCAAnimationRotateAuto

NSString * const kCAAnimationRotateAutoReverse

您是否尝试过设置:

keyframe.animationMode = kCAAnimationRotateAuto;

该文档指出:

kCAAnimationRotateAuto:对象沿路径的切线移动。

于 2012-03-03T07:34:09.440 回答