MD.
MD.

Reputation: 1151

how find position of CAlayer during animation?

I am implementing game application.In which i am using layer for animation.

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, previousValuex, previousValue);
CGPathAddLineToPoint(path, NULL, valuex, value);
previousValue=value;
previousValuex=valuex;

CAKeyframeAnimation *animation;
animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.path = path;
animation.duration =1.0;
animation.repeatCount = 0;
//animation.rotationMode = kCAAnimationRotateAutoReverse;
animation.calculationMode = kCAAnimationPaced;

// Create a new layer for the animation to run in.
CALayer *moveLayer = [imgObject layer];
[moveLayer addAnimation:animation forKey:@"position"];

Now i want to find layer position during animation?Is it possibel?please help me.

Upvotes: 1

Views: 4715

Answers (3)

petrocket
petrocket

Reputation: 1508

If your CALayer is inside another CALayer you may need to apply the affineTransform of the parent CALayer to get the position of the child CALayer like this:

// Create your layers
CALayer *child = CALayer.layer;
CALayer *parent = self.view.layer;
[parent addSubLayer:child];

// Apply animations, transforms etc...

// Child center relative to parent
CGPoint childPosition = ((CALayer *)child.presentationLayer).position;

// Parent center relative to UIView
CGPoint parentPosition = ((CALayer *)parent.presentationLayer).position;
CGPoint parentCenter = CGPointMake(parent.bounds.size.width/2.0, parent.bounds.size.height /2.0);

// Child center relative to parent center
CGPoint relativePos = CGPointMake(childPosition.x - parentCenter.x, childPosition.y - parentCenter.y);

// Transformed child position based on parent's transform (rotations, scale etc)
CGPoint transformedChildPos = CGPointApplyAffineTransform(relativePos, ((CALayer *)parent.presentationLayer).affineTransform);

// And finally...
CGPoint positionInView = CGPointMake(parentPosition.x +transformedChildPos.x, parentPosition.y + transformedChildPos.y);

This code is based on code I just wrote in which the parent CALayer was rotating and the position was changing and I wanted to get the position of a child CALayer relative to a touch position in the UIView the parent belonged to. So this is the basic idea, but I haven't actually run this pseudocode version.

Upvotes: 0

Brad Larson
Brad Larson

Reputation: 170319

In order to find the current position during an animation, you will need to look at the properties of the layer's presentationLayer. The properties of the layer itself will only reflect the final target value for an implicit animation, or the initial value before you applied the CABasicAnimation. The presentationLayer gives you the instantaneous value of whatever property you are animating.

For example,

CGPoint currentPosition = [[moveLayer presentationLayer] position];

will get you the current position of the layer as it is animating about your path. Unfortunately, I believe it is difficult to use key-value observing with the presentation layer, so you may need to manually poll this value if you want to track it.

Upvotes: 24

John Biesnecker
John Biesnecker

Reputation: 3812

I've never tried to do this, but you should be able to (perhaps via KVO?) monitor the CALayer's frame property (or position, or bounds, or anchorPoint, depending on what you need) during the animation.

Upvotes: 0

Related Questions