iphone - 如何为 View 或图像沿弯曲路径的移动设置动画?

我正在开发一个商务应用程序。当我将商品添加到购物车时,我想创建一种效果,其中商品的图像遵循弯曲路径并最终出现在购物车选项卡中。

如何沿着这样的曲线创建图像动画?

最佳答案

为了扩展 Nikolai 所说的内容,处理此问题的最佳方法是使用 Core Animation 沿 Bezier 路径为图像或 View 的运动设置动画。这是使用 CAKeyframeAnimation 完成的。例如,我使用以下代码将 View 的图像动画化为图标以指示保存(如 video for this application 所示):

首先导入QuartzCore头文件 #import <QuartzCore/QuartzCore.h>

UIImageView *imageViewForAnimation = [[UIImageView alloc] initWithImage:imageToAnimate];
imageViewForAnimation.alpha = 1.0f;
CGRect imageFrame = imageViewForAnimation.frame;
//Your image frame.origin from where the animation need to get start
CGPoint viewOrigin = imageViewForAnimation.frame.origin;
viewOrigin.y = viewOrigin.y + imageFrame.size.height / 2.0f;
viewOrigin.x = viewOrigin.x + imageFrame.size.width / 2.0f;

imageViewForAnimation.frame = imageFrame;
imageViewForAnimation.layer.position = viewOrigin;
[self.view addSubview:imageViewForAnimation];

// Set up fade out effect
CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
[fadeOutAnimation setToValue:[NSNumber numberWithFloat:0.3]];
fadeOutAnimation.fillMode = kCAFillModeForwards;
fadeOutAnimation.removedOnCompletion = NO;

// Set up scaling
CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
[resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(40.0f, imageFrame.size.height * (40.0f / imageFrame.size.width))]];
resizeAnimation.fillMode = kCAFillModeForwards;
resizeAnimation.removedOnCompletion = NO;

// Set up path movement
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
//Setting Endpoint of the animation
CGPoint endPoint = CGPointMake(480.0f - 30.0f, 40.0f);
//to end animation in last tab use 
//CGPoint endPoint = CGPointMake( 320-40.0f, 480.0f);
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, viewOrigin.x, viewOrigin.y);
CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, viewOrigin.y, endPoint.x, viewOrigin.y, endPoint.x, endPoint.y);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);

CAAnimationGroup *group = [CAAnimationGroup animation]; 
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
[group setAnimations:[NSArray arrayWithObjects:fadeOutAnimation, pathAnimation, resizeAnimation, nil]];
group.duration = 0.7f;
group.delegate = self;
[group setValue:imageViewForAnimation forKey:@"imageViewBeingAnimated"];

[imageViewForAnimation.layer addAnimation:group forKey:@"savingAnimation"];

[imageViewForAnimation release];

https://stackoverflow.com/questions/1142727/

相关文章:

objective-c - Objective-C中的ivars和属性有什么区别

ios - AFNetworking 发布请求

ios - 在自动布局中将 subview 的 X 居中抛出 "not prepared for t

objective-c - .h 和 .m 文件中的@interface 定义之间的区别

objective-c - 如何向 objc_exception_throw 添加断点?

ios - 没有为应用商店上的应用程序找到有效的 'aps-environment' 权利字符串

objective-c - 标题中有两行文本的 UIButton (numberOfLines=2)

objective-c - 我不想在开始更新、结束更新 block 中为 uitableview 设

iphone - NSTimeInterval 到 HH :mm:ss?

iphone - 如何找到 malloc "double free"错误的原因?