关键帧动画

时间:2011-05-09 06:42:06

标签: iphone cocoa-touch quartz-graphics

我正在尝试沿计算路径设置图像动画。我从以下代码中没有收到任何错误,但图像没有动画。有什么帮助吗?

UIImageView *rockHand = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"rockHand" ofType:@"png"]]];
rockHand.frame = CGRectMake(260, -8, 59, 66);
[self.view addSubview:rockHand];
[self.rockHands addObject:rockHand];

int x = 260;
int y = -8;
int spring = 15;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddLineToPoint(path, nil, x, y);
for (int i = 0; i < 10; i++) {
    y += (i*2) - spring;
    x += (i * -0.3) ;
    spring--;
    CGPathAddLineToPoint(path, nil, x, y);
}

CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim.path = path;
anim.duration = 3;
anim.fillMode = kCAFillModeForwards;
anim.calculationMode = kCAAnimationPaced;
anim.removedOnCompletion = NO;
[rockHand.layer addAnimation:anim forKey:@"pathAnimation"];

1 个答案:

答案 0 :(得分:1)

解决方案:必须放置第一个点才能添加其他点,所以

CGPathAddLineToPoint(path, nil, x, y);

必须是

CGPathMoveToPoint(path, nil, x, y);