我正在使用CAKeyframeAnimation
课程。我有两个图像沿着它们的曲线路径在屏幕上移动,我遇到了以下问题:偶尔当图像彼此接近时,其中一个(从来没有另一个)从一个或两个消失了屏幕。只有当它们接近时它才会发生,但当它们的帧相互作用时却不会发生。你有经历过这样的经历吗?可能是什么原因造成的?如果需要,我会显示我的代码。非常感谢。
这是第一张图片移动的方式:
- (void)lizardJumps
{
if (isAnimation)
return;
CGRect frame = [[stageFrames objectAtIndex:isLizardOnLeftRock ? 0 : 2] CGRectValue];
CGPoint start_point = CGPointMake(frame.origin.x, frame.origin.y);
CGRect frame2 = [[stageFrames objectAtIndex:isLizardOnLeftRock ? 2 : 0] CGRectValue];
CGPoint finish_point = CGPointMake(frame2.origin.x + 15, frame2.origin.y + 20);
CAKeyframeAnimation *lizardJumpAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, start_point.x, start_point.y);
CGPathAddQuadCurveToPoint(curvedPath, NULL, 240, 0, finish_point.x, finish_point.y);
lizardJumpAnimation.path = curvedPath;
lizardJumpAnimation.duration = jump_duration;
lizardJumpAnimation.fillMode = kCAFillModeForwards;
lizardJumpAnimation.removedOnCompletion = NO;
lizardJumpAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
lizardJumpAnimation.delegate = self;
[lizard.layer addAnimation:lizardJumpAnimation forKey:@"jumpAnimation"];
CGPathRelease(curvedPath);
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:jump_duration]];
lizard.transform = CGAffineTransformIdentity;
if (isLizardOnLeftRock)
lizard.transform = CGAffineTransformMakeScale(-1.0, 1.0);
else
lizard.transform = CGAffineTransformMakeScale(1.0, 1.0);
isLizardOnLeftRock = !isLizardOnLeftRock;
}
`
- (void)animationTiming:(NSTimer *)t
{
CALayer *testLayer = lizard.layer.presentationLayer;
CALayer *testBugLayer = bug.layer.presentationLayer;
BOOL intersects = CGRectIntersectsRect(testBugLayer.frame, testLayer.frame);
if (intersects)
NSLog(@"intersects");
}
- (void)animationDidStart:(CAAnimation *)anim
{
isAnimation = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(animationTiming:) userInfo:nil repeats:YES];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
isAnimation = NO;
[timer invalidate];
}
`
这是另一个:
- (void)bugFlies
{
[bug setHidden:NO];
CGPoint start_point = CGPointMake(0, 170);
CGPoint finish_point = CGPointMake(170, 170);
CAKeyframeAnimation *bugFlightAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
for (int i = 0; i < 3; i++)
{
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, start_point.x, start_point.y);
CGPathAddCurveToPoint(curvedPath, NULL, start_point.x + 50, finish_point.y + 100, start_point.x + 50 , finish_point.y - 100, finish_point.x, finish_point.y);
bugFlightAnimation.path = curvedPath;
bugFlightAnimation.duration = flight_duration;
bugFlightAnimation.removedOnCompletion = NO;
[bug.layer addAnimation:bugFlightAnimation forKey:@"bug_flight"];
start_point = finish_point;
finish_point.x += 170;
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:flight_duration]];
CGPathRelease(curvedPath);
}
[bug setHidden:YES];
}