我正在尝试使用核心动画和绘制实现一个简单形状的动画效果。这个形状由3条线和一条贝塞尔曲线组成。为了显示曲线控制点,还会绘制一条红线。
alt text http://img.skitch.com/20091119-1ufar435jdq7nwh8pid5cb6kmm.jpg
我的主控制器只是添加了这个子视图,并在touchesEnd
时调用adjustWave
方法。下面是我用于绘制形状的代码。你会发现这个类只有一个属性,cp1x(即贝塞尔曲线控制点1的x值)。我想要对它进行动画处理,但这只是一个愚蠢的尝试...
- (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];
}
形状没有改变。相反,如果我去掉CA代码并在最后一个方法中添加一个简单的`[self setNeedsDisplay]`,则形状会发生变化,但显然没有了CA。你能帮助我吗?我确定我在这里犯了一个非常基本的错误...谢谢您提前。