ios - CAAnimation 期间的位置

标签 ios xcode uibezierpath caanimation cgpath

我使用它来沿着路径为对象设置动画。我想知道如何在动画过程中的任意时刻获取对象的位置,以检查与另一个对象的碰撞。任何想法如何去做这个?谢谢。

- (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"];


}

最佳答案

我相信您有两个问题:

  • 检查碰撞
  • 找出验证碰撞的位置

检查碰撞

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

找出验证碰撞的位置

您可以使用每 1/60 秒运行一次的 NSTimer 来执行此操作。但是,这不是一个好的解决方案,因为 NSTimer 仅保证执行选择器所需的最小时间。通过这种方式,您可以在对象已经发生碰撞后检查碰撞的情况。

但是,您可以使用 CADisplayLink 。这样,您将在图层呈现在屏幕上之前接到电话。

关于ios - CAAnimation 期间的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20741057/

相关文章:

ios - 错误原因 : unrecognized selector sent to instance after add library 'Google-Mobile-Ads-SDK' , '~> 7.8'

ios - iOS 中的多点连接框架问题

objective-c - Xcode 4.2,无法运行单元测试

ios - 由于某种原因,UIBezierPath 轮廓不断出现在 View 周围

objective-c - 合并连接的 UIBezierPaths

ios - 我想在 ios 中使用具有不同数据的相同 UserInterface

ios - 如何保存 UIScrollView 的状态?

ios - Xcode 10 Alamofire 命令 PhaseScriptExecution 失败,退出代码非零

iphone - 在 iPhone 开发中单独设置宽度和高度

ios - SKSpriteNode 框架偏离