首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CAAnimation期间的位置

CAAnimation期间的位置
EN

Stack Overflow用户
提问于 2013-12-23 09:58:46
回答 1查看 161关注 0票数 0

我用这个在一条路径上给一个物体动画。我想知道如何获得对象的位置在动画期间的任何一点,以检查与另一个对象的碰撞。有什么办法吗?谢谢。

代码语言:javascript
复制
- (void) animateCicleAlongPath {
    //Prepare the animation - we use keyframe animation for animations of this complexity
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

    pathAnimation.calculationMode = kCAAnimationCubic;
    [pathAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];

    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.duration = 7.0;

    //Lets loop continuously for the demonstration
    pathAnimation.repeatCount = 1;

    //Setup the path for the animation - this is very similar as the code the draw the line
    //instead of drawing to the graphics context, instead we draw lines on a CGPathRef
    CGPoint endPoint = CGPointMake(endpointx, endpointy);
    CGMutablePathRef curvedPath = CGPathCreateMutable();

    CGPathMoveToPoint(curvedPath, NULL,startpointx,startpointy);


   // CGPathMoveToPoint(curvedPath, NULL, 10, 10);
     CGPathAddQuadCurveToPoint(curvedPath,NULL, controlpointx, controlpointy, endpointx, endpointy);




    //Now we have the path, we tell the animation we want to use this path - then we release the path
    pathAnimation.path = curvedPath;
    CGPathRelease(curvedPath);

    //We will now draw a circle at the start of the path which we will animate to follow the path
    //We use the same technique as before to draw to a bitmap context and then eventually create
    //a UIImageView which we add to our view
    UIGraphicsBeginImageContext(CGSizeMake(20,20));
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //Set context variables
    CGContextSetLineWidth(ctx, 1.5);
    CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor);
    CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
    //Draw a circle - and paint it with a different outline (white) and fill color (green)
    CGContextAddEllipseInRect(ctx, CGRectMake(1, 1, 18, 18));
    CGContextDrawPath(ctx, kCGPathFillStroke);
    UIImage *circle = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageView *circleView = [[UIImageView alloc] initWithImage:circle];
    circleView.frame = CGRectMake(1, 1, 20, 20);
    [throwingView addSubview:circleView];


    //Add the animation to the circleView - once you add the animation to the layer, the animation starts
    [circleView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-23 10:33:29

我相信你在这里有两个问题:

  • 检查碰撞
  • 找出验证碰撞的地方

检查碰撞

CALayer具有模型层和表示层。表示层为您提供所需的可视信息。基本上,该层负责获得有关该层在屏幕上的位置的信息。你可以让它做:circleView.layer.presentationLayer

找到验证碰撞的位置

您可以使用每隔1/60秒运行一次的NSTimer来完成此操作。但是,这不是一个好的解决方案,因为NSTimer只保证执行选择器所需的最小时间。这样,您就可以在对象已经被合并后检查您的colision了。

但是,您可以使用CADisplayLink。这样,在该层在屏幕上呈现之前,您将得到一个调用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20741057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档