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

本文转自老Zhan博客园博客,原文链接:http://www.cnblogs.com/mybkn/archive/2012/08/21/2648708.html,如需转载请自行联系原作者

相关文章
|
6月前
|
iOS开发
水波涟漪,使用SwiftUI做一个仿iPhone隔空投送动画~
水波涟漪,使用SwiftUI做一个仿iPhone隔空投送动画~
74 0
iPhone7、7P iOS10.2及以下系统转场动画出现白屏bug的解决办法
iPhone7、7P iOS10.2及以下系统转场动画出现白屏bug的解决办法
249 0
|
编解码 iOS开发
iphone 开发的基本入门知识
iphone 开发的基本入门知识
156 0
「镁客早报」iPhone或将在今年采用三摄;传Facebook致力于开发语音助力服务与亚马逊、苹果竞争
亚马逊向美国Alexa设备推免费音乐服务;视频会议软件开发商Zoom纳斯达克上市。
229 0