Positioning CALayer after moving an object

I am animating my UIImageView using CAAnimation using the layer property, for example:

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

But at the end of my animation (after the object has moved from the starting point), when I read the coordinates of the XY frame or the coordinates of the center, they always seem original, and not the ones the object moved to.

How to read the coordinates of a layer to 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]; 
+4
source share
2 answers

If you animate a layer, the view containing the layer will remain in one place, so the frame will remain the same. the layer also has border, border, and position properties, so try reading the layer properties.

EDIT:

I found the following explanation in the CAKeyframeAnimation Class Reference.

During the animation, it updates the property value in the render tree with values ​​calculated using the specified interpolation calculation mode.

and in order to find out what a rendering tree is, I found it here.

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

It seems that the values ​​are updated in the render tree, and the render tree is different from the view and the private one, we cannot access it.

Below is the last paragraph of the link above.

You can request a CALayer instance for the corresponding view layer during animation processing. This is most useful if you intend to change the current animation and want to start a new animation from the current displayed state.

At the heart of this work, you can calculate the new position of the layer based on the paths you provided and, accordingly, set the property of the presentation level frame.

Please write here if you find any update .....

+2
source

This is the self-learning declaration ( CALayer.h ):

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

The effect of trying to modify the returned layer in some way undefined.

The sublayers , mask and superlayer returned layer return the presentation versions of these properties. This is transferred to read-only levels. For example, calling -hitTest : based on the result, -presentationLayer will request a presentation of the value of the layer tree.

 - (id)presentationLayer; 
+1
source

All Articles