如何在我的iPad应用程序中通过不寻常的曲线pathanimation一个图像?

我有两个单独的图像。

  1. CurvedPath图像(附在下面)
  2. PersonImage

在这里输入图像说明

正如你所看到的,这个path对于单angular来说没有完美的弧度。

我有另一个PersonImage,我想要在这个弧形图像的中心线上方进行放大animation。

这个animation应该从左下angular开始到右上angular。

如何实现这种animation?

我已经阅读了关于QuartzCore和BeizerPath的animation,但由于我对这些知识的了解不多,所以我很难很快实现这一点。

沿着path移动

只要你可以得到你想要animation图像alignment的确切path,你可以使用核心animation和CAKeyframeAnimation。

为position属性创build一个关键帧animation,并设置它沿着你的path做animation

CAKeyframeAnimation *moveAlongPath = [CAKeyframeAnimation animationWithKeyPath:@"position"]; [moveAlongPath setPath:myPath]; // As a CGPath 

如果您将path创build为UIBezierpath,则可以通过在贝塞尔path上调用CGPath来轻松获取CGPath。

接下来你configuration持续时间等animation

 [moveAlongPath setDuration:5.0]; // 5 seconds // some other configurations here maybe... 

现在,将animation添加到imageView的图层,并沿着path进行animation处理。

 [[myPersonImageView layer] addAnimation:moveAlongPath forKey:@"movePersonAlongPath"]; 

如果你以前从未使用过Core Animation,那么你需要将QuartzCore.framework添加到你的项目中,并在你的实现的顶部添加#import <QuartzCore/QuartzCore.h>

创build一个UIBezierpath

如果你不知道什么是贝塞尔path,看看维基百科的网站。 一旦你知道你的控制点,你可以创build一个像这样的简单的贝塞尔path(所有的点都是普通的CGPoint):

 UIBezierPath *arcingPath = [UIBezierPath bezierPath]; [arcingPath moveToPoint:startPoint]; [arcingPath addCurveToPoint:endPoint controlPoint1:controlPoint1 controlPoint2:controlPoint2]; CGPathRef animationPath = [arcingPath CGPath]; // The path you animate along 

放大

要实现缩放效果,可以使用CABasicAnimation为图层转换应用类似的animation。 只需从0比例变换(无限小)变成1比例变换(正常大小)即可。

 CABasicAnimation *zoom = [CABasicAnimation animationWithKeyPath:@"transform"]; [zoom setFromValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.0, 0.0, 1.0);]; [zoom setToValue:[NSValue valueWithCATransform3D:CATransform3DIdentity]; [zoom setDuration:5.0]; // 5 seconds // some other configurations here maybe... [[myPersonImageView layer] addAnimation:zoom forKey:@"zoomPersonToNormalSize"]; 

两者同时

要将两个animation同时运行,请将其添加到animation组中,然后将其添加到人员图像视图中。 如果你这样做,那么你configurationanimation组(持续时间等,而不是个别的animation)。

 CAAnimationGroup *zoomAndMove = [CAAnimationGroup animation]; [zoomAndMove setDuration:5.0]; // 5 seconds // some other configurations here maybe... [zoomAndMove setAnimations:[NSArray arrayWithObjects:zoom, moveAlongPath, nil]]; [[myPersonImageView layer] addAnimation:zoomAndMove forKey:@"bothZoomAndMove"];