ios 簡單的文字字軌動畫

因為本人不想弄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 : 留郵箱 樓主私發

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容