gpt4 book ai didi

iphone - iPhone 开发中沿曲线移动对象

转载 作者:行者123 更新时间:2023-12-03 18:30:38 24 4
gpt4 key购买 nike

我想通过沿着特定曲线移动图像对象来为其设置动画。它不是一条一般的或随机的曲线,而是一条遵循屏幕上特定路径的曲线。

目前,我通过每次设置其框架来手动指定图像对象移动路径的 x 和 y 坐标列表。这是一个费力的过程,因为我要设置路径的特定 x 和 y 坐标并沿其移动图像。有没有更有效的方法来做到这一点?

有没有一种方法可以让我指定大约 15 - 20 个点,并沿着这些点绘制一条曲线来移动对象?还有其他方法可以实现这一目标吗?任何帮助将非常感激。谢谢。

最佳答案

您可以结合使用 UIBezierPath 和 CAKeyFrameAnimation。我发现了一篇关于这个主题的非常有用的博客文章。

http://oleb.net/blog/2010/12/animating-drawing-of-cgpath-with-cashapelayer/

这是我使用的简化版本(它只是对正方形的绘制进行动画处理):

UIBezierPath *customPath = [UIBezierPath bezierPath];
[customPath moveToPoint:CGPointMake(100,100)];
[customPath addLineToPoint:CGPointMake(200,100)];
[customPath addLineToPoint:CGPointMake(200,200)];
[customPath addLineToPoint:CGPointMake(100,200)];
[customPath addLineToPoint:CGPointMake(100,100)];

UIImage *movingImage = [UIImage imageNamed:@"foo.png"];
CALayer *movingLayer = [CALayer layer];
movingLayer.contents = (id)movingImage.CGImage;
movingLayer.anchorPoint = CGPointZero;
movingLayer.frame = CGRectMake(0.0f, 0.0f, movingImage.size.width, movingImage.size.height);
[self.view.layer addSublayer:movingLayer];

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = 4.0f;
pathAnimation.path = customPath.CGPath;
pathAnimation.calculationMode = kCAAnimationLinear;
[movingLayer addAnimation:pathAnimation forKey:@"movingAnimation"];

关于iphone - iPhone 开发中沿曲线移动对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4538279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com