因为本人不想弄gif图 所有就这样简陋的给大家看代码吧,最下面附上丑陋的代码。
一开始先写个全局属性
@property(nonatomic,strong)CAShapeLayer*shapeLayer;
接下来重点:
CGMutablePathRef letters =CGPathCreateMutable(); //保存所有文字的路径
NSAttributedString *str = [[NSAttributedStringalloc]initWithString:@“testtesttesttest”];
CTLineRef line =CTLineCreateWithAttributedString((CFAttributedStringRef)str);//获取str有多少行
CFArrayRef arrRef =CTLineGetGlyphRuns(line);//从一行中得到ref数组
获取完数组后便执行下面的代码:
接下来在循环外面写入下列代码:
CFRelease(line);
UIBezierPath*path = [UIBezierPathbezierPath];
[pathmoveToPoint:CGPointZero];
[path appendPath:[UIBezierPathbezierPathWithCGPath:letters]];
CGPathRelease(letters);
CAShapeLayer*pathLayer = [CAShapeLayerlayer];
pathLayer.frame=self.bounds;
pathLayer.bounds=CGPathGetBoundingBox(path.CGPath);
pathLayer.geometryFlipped=YES;
pathLayer.path= path.CGPath;
pathLayer.strokeColor= [self.colorCGColor];
pathLayer.fillColor=nil;
pathLayer.lineWidth=1;
pathLayer.lineJoin=kCALineJoinBevel;
self.shapeLayer= pathLayer;
self.shapeLayer.strokeEnd=0;
[self.layer addSublayer:self.shapeLayer];
记得 在layer中有个属性strokeEnd,这属性代表的是layer的完成度,圆形进度条也是利用这属性做的当strokeEnd = 0 时,layer将不显示,当为1时将显示完整,即我们可以:CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.fillMode=kCAFillModeForwards;
pathAnimation.removedOnCompletion=NO;
pathAnimation.duration=self.time;
pathAnimation.fromValue= [NSNumber numberWithFloat:self.startStrok];
pathAnimation.toValue= [NSNumber numberWithFloat:self.endStrok];
[self.shapeLayer addAnimation:pathAnimationforKey:@"strokeEnd"];
好的 大功告成了!
关于demo : 留邮箱 楼主私发
网友评论