在UIView中为线条绘制设置动画

时间:2011-04-02 22:07:44

标签: iphone cocoa-touch uiview uikit core-animation

任何人都可以帮我解决这个问题吗?我有线条的nsarray,我需要在一些UIView中一个接一个地编写它们(我想为手绘图设置动画)。

4 个答案:

答案 0 :(得分:6)

以下是答案(见http://soulwithmobiletechnology.blogspot.fr/2012/07/how-to-animate-line-draw.html

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(50.0,0.0)];
[path addLineToPoint:CGPointMake(120.0, 600.0)];

CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = self.view.bounds;
pathLayer.path = path.CGPath;
pathLayer.strokeColor = [[UIColor redColor] CGColor];
pathLayer.fillColor = nil;
pathLayer.lineWidth = 2.0f;
pathLayer.lineJoin = kCALineJoinBevel;

[self.view.layer addSublayer:pathLayer];

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 2.0;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
[pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];

不要忘记#import <QuartzCore/QuartzCore.h>

答案 1 :(得分:0)

可能会有所帮助的一些切入点:

答案 2 :(得分:0)

此代码通过一组点动画对象。可以对你的线使用相同的想法,我认为是CGPath?

//
// animate along a set of points
// 
// NSLog(@"The content of movement1PointsArray is%@",movement1PointsArray);     
CGMutablePathRef touchPath1 = CGPathCreateMutable();
CGPoint touchPath1StartPoint = [[movement1PointsArray objectAtIndex:0] CGPointValue];
CGPathMoveToPoint(touchPath1,NULL,touchPath1StartPoint.x, touchPath1StartPoint.y);

for (NSInteger p = 0; p < [movement1PointsArray count]; ++p)
{
    CGPoint touchesPointOnPath = [[movement1PointsArray objectAtIndex:p] CGPointValue];
    CGPathAddLineToPoint(touchPath1, NULL,touchesPointOnPath.x,touchesPointOnPath.y);   
}
CAKeyframeAnimation* touchPathAnimation1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
[touchPathAnimation1 setDuration: 1.0];
[touchPathAnimation1 setAutoreverses: NO];
touchPathAnimation1.removedOnCompletion = NO;
touchPathAnimation1.fillMode = kCAFillModeForwards;
[touchPathAnimation1 setPath: touchPath1];
CFRelease(touchPath1);
[animationsArray addObject:touchPathAnimation1];
[ball.layer addAnimation: touchPathAnimation1 forKey: @"position"];
  • 我无法获得动画的第二条路径...无论我尝试什么,它只会动画最后一条路径。

答案 3 :(得分:-1)

您最终可能会实现视图drawRect方法。 这http://howtomakeiphoneapps.com/2009/08/how-to-draw-shapes-with-core-graphics/

应该让你入门