设置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

 

相关文章:

  • 2021-05-28
  • 2022-12-23
  • 2021-08-03
  • 2022-12-23
  • 2022-12-23
  • 2021-05-20
  • 2021-09-01
猜你喜欢
  • 2021-08-28
  • 2022-01-07
  • 2022-12-23
  • 2021-10-22
  • 2021-06-08
  • 2021-07-05
相关资源
相似解决方案