Me estoy acercando a la animación principal y al dibujo empíricamente. Estoy tratando de animar una forma simple; la forma en cuestión está formada por 3 líneas más una curva bezier. También se dibuja una línea roja para mostrar los puntos de control de la curva.Animando una forma con CoreAnimation
alt text http://img.skitch.com/20091119-1ufar435jdq7nwh8pid5cb6kmm.jpg
Mi controlador principal se limita a añadir esta subvista y llama al método adjustWave
siempre touchesEnd
. Aquí está el código para mi clase de dibujo de formas. Como puede ver, la clase tiene una propiedad, cp1x (la x del punto de control 1 de bezier). Este es el valor que me gustaría animar. Cuidado, este es un intento tonto ...
- (void)drawRect:(CGRect)rect {
float cp1y = 230.0f;
float cp2x = 100.0f;
float cp2y = 120.0f;
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 10.0f, 200.0f);
CGPathAddCurveToPoint (path, NULL, cp1x, cp1y, cp2x, cp2y, 300.0f, 200.0f);
CGPathAddLineToPoint(path, NULL, 300.0f, 300.0f);
CGPathAddLineToPoint(path, NULL, 10.0f, 300.0f);
CGPathCloseSubpath(path);
CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
CGContextAddPath(ctx, path);
CGContextFillPath(ctx);
// Drawing a line from control points 1 and 2
CGContextBeginPath(ctx);
CGContextSetRGBStrokeColor(ctx,1,0,0,1);
CGMutablePathRef cp1 = CGPathCreateMutable();
CGPathMoveToPoint(cp1, NULL, cp1x, cp1y);
CGPathAddLineToPoint(cp1, NULL, cp2x, cp2y);
CGPathCloseSubpath(cp1);
CGContextAddPath(ctx, cp1);
CGContextStrokePath(ctx);
}
- (void)adjustWave {
[UIView beginAnimations:@"movement" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationWillStartSelector:@selector(didStart:context:)];
[UIView setAnimationDidStopSelector:@selector(didStop:finished:context:)];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:3.0f];
[UIView setAnimationRepeatCount:3];
[UIView setAnimationRepeatAutoreverses:YES];
cp1x = cp1x + 20.0f;
[UIView commitAnimations];
}
La forma no cambia. Si, por el contrario, saco el código CA y agrego un simple `[self setNeedsDisplay] en el último método, la forma cambia, pero obviamente sin CA. ¿Puedes ayudar a sme? Estoy seguro de que estoy cometiendo un error muy básico aquí ... Gracias de antemano, Davide
Como dice Brad, puede usar la clase CAShapeLayer para animar una forma arbitraria construyendo una CABasicAnimation o CAKeyframeAnimation y animando por la propiedad 'path'. Escribí una publicación sobre esta clase si quieres ver el código de muestra. http://bit.ly/shapelayer –