对象移动后的CALayer定位 [英] CALayer Positioning After Object Has Moved

查看:70
本文介绍了对象移动后的CALayer定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I animate my UIImageView with CAAnimation using layer property like so:

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

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

But at the end of my animation (after object has moved from original point) when I read the frame's X Y coordinates or center coordinates they always appear to be original ones, not those where the object has moved to.

如何读取图层的坐标,以便确定正确的位置

How to read layer's coordinates so I determine the correct location of my moving object?

这是我的代码:

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


推荐答案

如果为图层设置动画,包含该图层的视图将保持在同一位置,因此框架将保持不变。图层还具有框架,边界和位置属性,因此请尝试读取图层的属性。

If you animate layer, the view that contains layer will remain at same place so frame will remain same. layer also have frame, bound and position properties, so try reading properties of layer.

编辑:

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

I found following explanation in 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.

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

and for knowing what is render tree I found it here.

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

似乎值正在渲染树中更新,并且由于渲染树不同于表示形式和私有形式,因此我们无法

Seems that values are getting updated in render tree and as render tree is different from presentation and private we cannot access it.

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

Following is last paragraph of above link.


您可以在进行动画交易时,查询CALayer的实例以获取其对应的表示层。如果您打算更改当前动画并希望从当前显示的状态开始新动画,这将非常有用。

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.

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

Baseed on this a work around can be to calculate new position of layer based on the paths you provided and setting presentation layer frame property accordingly.

请在此处填写,以防发现任何更新.....

Please write here in case you find any update.....

这篇关于对象移动后的CALayer定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆