![](/img/trans.png)
[英]How to get position during animation which uses CALayer and CGPath in iPhone
[英]how find position of CAlayer during 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"];
现在我想在动画过程中找到图层的位置?是可能的吗?请帮帮我。
为了在动画过程中找到当前位置,您将需要查看图层的presentationLayer
的属性。 图层本身的属性将仅反映隐式动画的最终目标值,或应用CABasicAnimation
之前的初始值。 presentationLayer
为您提供动画的任何属性的瞬时值。
例如,
CGPoint currentPosition = [[moveLayer presentationLayer] position];
会在显示动画路径时为您提供该图层的当前位置。 不幸的是,我认为很难在表示层中使用键值观察,因此,如果要跟踪它,可能需要手动轮询该值。
我从未尝试过这样做,但是您应该能够在动画过程中(也许通过KVO吗?)监视CALayer的frame
属性(或position
, bounds
或anchorPoint
,具体取决于您的需求)。
如果您的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);
这段代码基于我刚才编写的代码,其中父CALayer旋转并且位置发生了变化,我想获得子CALayer相对于父所属的UIView中的触摸位置的位置。 所以这是基本思想,但我实际上没有运行此伪代码版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.