关键帧动画

发布于 2024-11-05 20:37:55 字数 924 浏览 2 评论 0原文

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

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

I'm trying to animate an image along a calculated path. I receive no error from the following code, but the image doesn't animate. Any help?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疾风者 2024-11-12 20:37:55

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

CGPathAddLineToPoint(path, nil, x, y);

必须

CGPathMoveToPoint(path, nil, x, y);

Solution: one must place a first point in order to add others, so

CGPathAddLineToPoint(path, nil, x, y);

has to be

CGPathMoveToPoint(path, nil, x, y);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文