iphone - 对象移动后的 CALayer 定位

标签 iphone objective-c uiimageview calayer caanimation

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

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

但是在我的动画结束时(在对象从原始点移动之后)当我读取帧的 X Y 坐标或中心坐标时,它们总是显示为原始坐标,而不是对象移动到的坐标。

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

这是我的代码:

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];

最佳答案

如果您为图层设置动画,包含图层的 View 将保留在同一位置,因此帧将保持不变。层也有 frame、bound 和 position 属性,因此请尝试阅读层的属性。

编辑:

我在 CAKeyframeAnimation Class Reference 中找到了以下解释。

While animating, it updates the value of the property in the render tree with values calculated using the specified interpolation calculation mode.

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

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

似乎渲染树中的值正在更新,并且由于渲染树不同于演示文稿并且是私有(private)的,我们无法访问它。

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

You can query an instance of CALayer for its corresponding presentation layer while an animation transaction is in process. This is most useful if you intend to change the current animation and want to begin the new animation from the currently displayed state.

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

如果您发现任何更新,请写在这里......

关于iphone - 对象移动后的 CALayer 定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7336529/

相关文章:

ios - 单击多个单元格上的 UItableview 正在快速检查 3

iphone - 将字符串中每个单词的第一个字符更改为大写

iphone - NSURLRequest http协议(protocol)版本

ios - 试图管理大型 UITableView 数据结构

ios - 在 uiimageview 中动画图像

ios - UITableViewCell 的 UIImage 的内存在 segue 后退按钮后未释放

iphone - iPhone 的加速度计有多精确——是否可以绘制出相对准确的手机位置运动路径?

iphone - UITextView 不响应另一个 UITextView 附件 View iPhone?

ios - NSRange block StartRange

iphone - 如何检查 UIImageView 是否出现在 iphone 中的 View 位置