iphone:实现像DrawSomething类似的自动画图的动画效果

设置layer的path为NSBezierPath对象,再对layer添加animation,注意
[CABasicAnimation animationWithKeyPath:@"strokeEnd"] 中的keyPath不能随意更改。

CAShapeLayer *l = [CAShapeLayer layer];
l.frame = self.view.bounds;
l.strokeColor = [UIColor redColor].CGColor;
CGPoint start = CGPointMake(arc4random()%300+10, arc4random()%400+40);
CGPoint end = CGPointMake(arc4random()%300+10, arc4random()%400+40);
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:start];
[path addLineToPoint:end];
l.path = path.CGPath;
[path release];

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat:1.0f];
animation.duration = 3.0f;
[l addAnimation:animation forKey:@"myStroke"];
[self.view.layer addSublayer:l];

 

check:http://stackoverflow.com/questions/7966590/iphone-core-animation-animate-a-nsbezierpath

也可以参考一份代码:https://github.com/ole/Animated-Paths 其实现的效果在code4app中有介绍http://code4app.com/ios/Animated-Paths/5020854c6803faa152000000

 

posted on 2012-08-21 11:05  老Zhan  阅读(1906)  评论(0编辑  收藏  举报