Ios 飞行中的位置
我用它来制作一个物体沿着路径的动画。我想知道如何在动画过程中的任意点获取对象的位置,以检查是否与另一个对象发生碰撞。你知道怎么做吗?谢谢Ios 飞行中的位置,ios,xcode,uibezierpath,caanimation,cgpath,Ios,Xcode,Uibezierpath,Caanimation,Cgpath,我用它来制作一个物体沿着路径的动画。我想知道如何在动画过程中的任意点获取对象的位置,以检查是否与另一个对象发生碰撞。你知道怎么做吗?谢谢 - (void) animateCicleAlongPath { //Prepare the animation - we use keyframe animation for animations of this complexity CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimat
- (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
只保证执行选择器所需的最短时间。通过这种方式,您可以在对象已合并后检查合并情况
但是,您可以使用。这样,在屏幕上渲染层之前,您将收到一个调用。是的,这就是我刚才想出来的。我和。。。animated.frame=[[animated.layer presentationLayer]frame];我来看看CADDisplay链接谢谢。你也可以看看Nick关于Advanced Core动画的洛克伍德书籍。:)那本书太棒了:)。谢谢你的推荐@JackyBoy@TiagoAlmeida“.NSTimer只保证执行选择器所需的最短时间…”您能告诉我更多信息吗?提前感谢。@Unheilig当您使用NSTimer时,您说的是“X秒后执行此选择器”。您不会说“在X秒时执行此选择器”。每个线程都有一个主nsrunlop。此nsrunlop执行一组任务,您的NSTimer将插入该列表中。但是,您无法保证每个任务所需的时间,包括您正在寻找的现代Swift中的NSTimer(尽管可以保证它至少需要您定义的秒数)
thatLayer.presentation()