简体   繁体   中英

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.

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.

我从未尝试过这样做,但是您应该能够在动画过程中(也许通过KVO吗?)监视CALayer的frame属性(或positionboundsanchorPoint ,具体取决于您的需求)。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM