0

我有一个图层,CAKeyframeAnimation它沿着路径对其进行动画处理。transform当动画rotationMode设置为 时,图层也有一个变化的属性kCAAnimationRotateAuto

当我点击图层时(我正在使用点击手势识别器进行检测并像在这个问题presentationLayer中一样对动画图层进行点击测试,如果有更好的方法让我知道),我想做以下事情:

  1. 从表示层抓取位置和变换
  2. 移除当前动画。
  3. 使用在步骤 1 中获取的信息,向图层添加一个新动画,以保持变换和起点,并减慢图层到新端点的传输速度,距离上一个动画刚刚结束的位置约 40 像素。

最终结果应该是,当点击时,动画层将沿着一条直线减速到停止,该直线与删除原始关键帧动画的点处的路径相切。

在数学方面,我该怎么做?我已经有了起点和变换,所以在我看来,我只需要找出终点,也许可以通过使用起点、变换和距离(为了好玩,比如 40)。有任何想法吗?

4

1 回答 1

2

If I understand correctly what you basically want is for the animated layer to decelerate and stop along the same vector as it's moving when you tap it. If you have the position when you tap it and the transform when you tap it I think you can find out the endpoint by doing the following:

CATransform3D transform = <Your layer's transform goes here>;
CGPoint startPoint = <Your layers's current position goes here>;
CGFloat distance = 40.f;

CGPoint v = CGPointMake(0, distance);

CGAffineTransform affineTransform = CATransform3DGetAffineTransform(transform);

CGPoint offset = CGPointApplyAffineTransform(v, affineTransform);
CGPoint endPoint = CGPointMake(startPoint.x + offset.x, startPoint.y + offset.y);

P.S. - This only works if you don't apply any scaling, skewing etc. as part of your animation (i.e. the transform should only represent rotations and translations)

于 2012-06-21T14:37:46.280 回答