在iPhone开发中沿着曲线移动对象 [英] Moving an object along a curve in iPhone development

查看:92
本文介绍了在iPhone开发中沿着曲线移动对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wanted to animate an image object by moving it along a particular curve. It is not a general or random curve but rather a curve which follows a particular path on screen.

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

Currently, Im manually specifying the list of x and y co-ordinates of the path along which i want the image object to move by setting its frame each time. This is a laborious process in the sense that im setting the specific x and y coordinates of the path and moving the image along it. Is there a more efficient way to do this?

有没有一种方法我可以指定,比方说,只有大约15 - 20点,并有一条曲线跟踪到这些移动物体?有没有其他方法可以实现这一目标?任何帮助将非常感激。谢谢。

Is there a way that i can specify,say, just about 15 - 20 points and have a curve traced along those to move the object? Any other way to acheive this? Any help would be much appreciated. Thanks.

推荐答案

您可以使用UIBezierPath和CAKeyFrameAnimation的组合。
我找到了一篇非常有用的博客文章来处理这个问题。

You could use a combination of UIBezierPath and CAKeyFrameAnimation. I found a very useful blog post dealing with this subject.

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

这是我使用的简化版本(它只是为正方形绘制动画):

Here's a simplified version of what I used (it just animates the drawing of a square):

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开发中沿着曲线移动对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆