因為本人不想弄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數(shù)組
獲取完數(shù)組后便執(zhí)行下面的代碼:
接下來在循環(huán)外面寫入下列代碼:
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 : 留郵箱 樓主私發(fā)