4

我使用图层属性使用 CAAnimation 为我的 UIImageView 设置动画,如下所示:

    [imageView.layer addAnimation:pathAnimation forKey:[NSString stringWithFormat:@"pathAnimation%@",objId]];

但是在我的动画结束时(在对象从原始点移动之后),当我读取框架的 XY 坐标或中心坐标时,它们总是看起来是原始坐标,而不是对象移动到的那些坐标。

如何读取图层的坐标以便确定移动对象的正确位置?

这是我的代码:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];  
imageView.image = [UIImage imageNamed:@"banner1.jpg"];
imageView.animationDuration = 2;
imageView.animationRepeatCount = 0;
imageView.tag = 100000;

imageView.layer.frame = imageView.frame;

[self.view addSubview:imageView];   
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = 2.0f;
pathAnimation.delegate=self;
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO; 
CGMutablePathRef pointPath = CGPathCreateMutable();
CGPathMoveToPoint(pointPath, NULL, 100, 100);
CGPathAddLineToPoint(pointPath, NULL, 300, 200);
CGPathAddLineToPoint(pointPath, NULL, 200, 150);
pathAnimation.path = pointPath;
CGPathRelease(pointPath);   
[imageView.layer addAnimation:pathAnimation forKey:@"pathAnimation"];
[imageView release];
4

2 回答 2

3

如果您为图层设置动画,则包含图层的视图将保持在同一位置,因此框架将保持不变。layer 也有 frame、bound 和 position 属性,所以尝试读取 layer 的属性。

编辑:

我在 CAKeyframeAnimation 类参考中找到了以下解释。

在制作动画时,它会使用使用指定插值计算模式计算的值更新渲染树中的属性值。

为了知道什么是渲染树,我在这里找到了它。

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/CoreAnimationArchitecture.html#//apple_ref/doc/uid/TP40006655-SW1

似乎渲染树中的值正在更新,并且由于渲染树与表示和私有不同,我们无法访问它。

以下是上述链接的最后一段。

您可以在动画事务处理过程中查询 CALayer 实例以获取其对应的表示层。如果您打算更改当前动画并希望从当前显示的状态开始新动画,这将非常有用。

基于此,一种解决方法可以是根据您提供的路径计算层的新位置并相应地设置表示层框架属性。

请写在这里,以防您发现任何更新......

于 2011-09-07T16:04:11.807 回答
1

这是自我解释的声明(CALayer.h):

返回包含当前事务开始时所有属性的图层副本,并应用任何活动动画。这给出了当前显示的图层版本的近似值。如果层尚未提交,则返回 nil。

尝试以任何方式修改返回层的效果是不确定的。

返回层的sublayers和属性返回这些属性的表示版本masksuperlayer这贯穿于只读层方法。例如,调用-hitTest: 的结果-presentationLayer将查询层树的表示值。

- (id)presentationLayer;
于 2012-04-18T21:21:38.457 回答