简体   繁体   English

如何在动画过程中找到CAlayer的位置?

[英]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 . 为了在动画过程中找到当前位置,您将需要查看图层的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 . 图层本身的属性将仅反映隐式动画的最终目标值,或应用CABasicAnimation之前的初始值。 The presentationLayer gives you the instantaneous value of whatever property you are animating. presentationLayer为您提供动画的任何属性的瞬时值。

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: 如果您的CALayer在另一个CALayer中,则可能需要应用父CALayer的affineTransform来获得子CALayer的位置,如下所示:

// 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. 这段代码基于我刚才编写的代码,其中父CALayer旋转并且位置发生了变化,我想获得子CALayer相对于父所属的UIView中的触摸位置的位置。 So this is the basic idea, but I haven't actually run this pseudocode version. 所以这是基本思想,但我实际上没有运行此伪代码版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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