CALayer Positioning After Object Has Moved
Asked Answered
H

2

4

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];
Homestretch answered 7/9, 2011 at 15:33 Comment(0)
T
3

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

Toponym answered 7/9, 2011 at 16:4 Comment(6)
I tried reading bounds like so NSLog(@"%f %f",imageView.layer.bounds.origin.x, imageView.layer.frame.origin.x); but I receive [0.00 10.00] where 10 is correct but still original point - not final destination point. OK I will try "position" right now...Homestretch
Position property did not work either. I used imageView.layer.position.x but result is strange: for my original view at (10,10,50,50) position becomes always 35 while object is moved to 100, 200, etc. Am I doing something wrong?Homestretch
position points to center of the layer. So it seems that 35 is again original position.Toponym
check this if it is helpful. #630765Toponym
useful reference but i need to use a complex path to move object on with multiple points. UIView animation, as I understand, does not do it.Homestretch
I did some more work and updated the answer. Please post if you have better information. It would be useful for me too.Toponym
J
1

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;
Josefajosefina answered 18/4, 2012 at 21:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.