对象移动后 CALayer 定位

发布于 2024-12-03 12:57:35 字数 1298 浏览 0 评论 0原文

我使用图层属性使用 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];

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

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

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?

Here is my code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁人与我共长歌 2024-12-10 12:57:35

如果对图层进行动画处理,包含图层的视图将保留在同一位置,因此帧将保持相同。图层还具有框架、绑定和位置属性,因此请尝试读取图层的属性。

编辑:

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

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

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

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

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

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

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

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

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

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.

EDIT:

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.

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.....

情绪失控 2024-12-10 12:57:35

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

返回包含所有属性的图层副本
在当前事务开始时,带有任何活动动画
应用。这给出了图层版本的近似值
当前显示的内容。如果该层还没有,则返回 nil
已承诺。

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

返回的 sublayersmasksuperlayer 属性
层返回这些属性的表示版本。这
延续到只读层方法。例如,调用 -hitTest
-presentationLayer 的结果上将查询演示文稿
图层树的值。

- (id)presentationLayer;

This is self explaining declaration (CALayer.h):

Returns a copy of the layer containing all properties as they were
at the start of the current transaction, with any active animations
applied. This gives a close approximation to the version of the layer
that is currently displayed. Returns nil if the layer has not yet
been committed.

The effect of attempting to modify the returned layer in any way is
undefined.

The sublayers, mask and superlayer properties of the returned
layer return the presentation versions of these properties. This
carries through to read-only layer methods. E.g., calling -hitTest:
on the result of the -presentationLayer will query the presentation
values of the layer tree.

- (id)presentationLayer;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文