【问题标题】:How to get position during animation which uses CALayer and CGPath in iPhone如何在 iPhone 中使用 CALayer 和 CGPath 的动画期间获取位置
【发布时间】:2011-04-20 06:32:53
【问题描述】:
 hI,

 I am workng on animation using CALayer and CAPath.

 Here I am animating an Image from top to bottom of screen.I gave starting and ending position for
 the animation.Now, I want to get position  during the animation.

Sample Code:

   - (void)onTimer
    {

    CGImageRef imageRef = [[UIImage imageNamed:@"apple.png"] CGImage];
int startX                      = round(random() % 460);
double speed     = 1 / round(random() %100) + 1.0;

layer          = [CALayer layer];
layer.name              = @"layer";
layer.contents          = (id)imageRef; // cached image
layer.frame             = CGRectMake(startX, self.view.frame.size.height+10,    CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));

[mView.layer addSublayer:layer];


 path = CGPathCreateMutable(); 
CGPathMoveToPoint(path, NULL, startX, -100);
CGPathAddLineToPoint(path, NULL, startX, self.view.frame.size.height+10);



CAKeyframeAnimation *animation= [CAKeyframeAnimation animationWithKeyPath:@"position"];

animation.delegate=self;

animation.duration= 10*speed;
animation.autoreverses=NO;
animation.path=path;
animation.repeatCount = 0;
animation.removedOnCompletion=YES;
animation.calculationMode = kCAAnimationLinear;

[layer addAnimation:animation forKey:@"position"];


CGPoint point= CGPathGetCurrentPoint(path);


   }

    In this I used "CGPathGetCurrentPoint" method but it did not work. Also used 
     presentationLayer method of CALayer but uit also did not work in this case.

     Can anyone suggest the code to get the position during animation
    thanks in advance

【问题讨论】:

    标签: iphone objective-c calayer cgpath cakeyframeanimation


    【解决方案1】:

    您可以通过查看图层的presentationLayer 上的属性来获取动画期间在屏幕上看到的内容的值。

    【讨论】:

      【解决方案2】:

      Core Animation 不是为此而设计的。它的工作是为你处理属性动画,而不是让你接触它。

      如果你想知道它在动画过程中的位置,那么你需要在没有Core Animation的情况下自己完成所有的动画。

      CGPathGetCurrentPoint() 是一个访问CGPath 实例的控制点的函数,而不是Core Animation 关键帧动画路径。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-18
        • 1970-01-01
        • 1970-01-01
        • 2013-05-02
        • 1970-01-01
        • 1970-01-01
        • 2014-07-29
        • 1970-01-01
        相关资源
        最近更新 更多