Hi all, I am developing a commerce application. When I add an item to the cart, I want to create an effect where an image of the item follows a curve path and ends up at the cart tab. This can be seen in Barnes and Nobles Bookstore app in the App store.
+1
A:
You can animate a UIView's center property using a CAKeyframeAnimation. See the CoreAnimation programming guide.
Nikolai Ruhe
2009-07-17 11:45:57
+16
A:
To expand upon what Nikolai said, the best way to handle this is to use Core Animation to animate the motion of the image or view along a Bezier path. This is accomplished using a CAKeyframeAnimation. For example, I've used the following code to animate an image of a view into an icon to indicate saving (as can be seen in the video for this application):
UIImageView *imageViewForAnimation = [[UIImageView alloc] initWithImage:imageToAnimate];
imageViewForAnimation.alpha = 1.0f;
CGRect imageFrame = imageViewForAnimation.frame;
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;
CGPoint endPoint = CGPointMake(480.0f - 30.0f, 40.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];
Brad Larson
2009-07-17 12:56:26
I got it.But i want to create animation effect something like i am having big image i should get resized image to big image's center then add animation path.How to get it.
2009-07-20 07:00:58
I'm not sure what you're asking, but the animation above does three things: moves the image along a curved path, shrinks it as it moves so that it's tiny by the time it hits the end point, and fades it out as it moves. You can tweak the resizing by changing the from or to values, as needed.
Brad Larson
2009-07-20 12:35:11
What do I have to include so that Xcode recognizes all the CAKey...stuff? It says it doesnt find the symbols.
Thanks
2009-07-21 12:36:44
You need to include what you always do for Core Animation, QuartzCore/QuartzCore.h, and link against the QuartzCore framework.
Brad Larson
2009-07-21 12:48:23
That was the solution. Perfect!
Thanks
2009-07-21 13:12:18
This should be a comment on my answer, not a separate answer by itself. In my answer, viewOrigin would be a CGPoint corresponding to the initial origin of the image. It is not shown in the above code sample, but it is trivial to obtain.
Brad Larson
2010-08-27 22:04:27