2009-07-17 12 views
78

Estoy desarrollando una aplicación de comercio. Cuando agrego un artículo al carrito de la compra, quiero crear un efecto donde una imagen del artículo sigue una ruta curva y termina en la pestaña del carrito.¿Cómo puedo animar el movimiento de una vista o imagen a lo largo de una trayectoria curva?

¿Cómo puedo crear una animación de una imagen a lo largo de una curva como esta?

+0

[Imágenes animadas en el iPhone] (http://www.cuppadev.co.uk/animated-images-on-the-iphone-sans-memory-leaks/) Consulte este enlace. Te ayudará seguramente. Gracias. – SALMAN

Respuesta

2

Puede animar una propiedad del centro de UIView utilizando una CAKeyframeAnimation. Consulte la guía de programación CoreAnimation.

152

Para ampliar lo que dijo Nikolai, la mejor manera de manejar esto es utilizar Core Animation para animar el movimiento de la imagen o ver a lo largo de una ruta Bezier. Esto se logra usando una CAKeyframeAnimation. Por ejemplo, he utilizado el código siguiente para animar una imagen de una vista en un icono para indicar el ahorro (como puede verse en el video for this application):

En primer lugar el archivo de cabecera QuartzCore importación #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]; 
+0

Lo tengo. Pero quiero crear un efecto de animación, algo así como que estoy teniendo una imagen grande. Debería cambiar el tamaño de la imagen al centro de la imagen grande y luego agregar la ruta de la animación. Cómo conseguirlo. –

+0

No estoy seguro de lo que está preguntando, pero la animación anterior hace tres cosas: mueve la imagen a lo largo de una trayectoria curva, la reduce a medida que se mueve para que sea pequeña cuando llega al punto final y la desaparece mientras se mueve. Puede ajustar el cambio de tamaño cambiando los valores de o a, según sea necesario. –

+0

¿Qué debo incluir para que Xcode reconozca todo el CAKey ... cosas? Dice que no encuentra los símbolos. – Thanks

Cuestiones relacionadas