2
圖紙我目前使用此代碼,以動畫人物的繪製:動畫的信件,CGPaths和CAShapeLayers
var path = UIBezierPath()
var unichars = [UniChar]("J".utf16)
var glyphs = [CGGlyph](count: unichars.count, repeatedValue: 0)
let gotGlyphs = CTFontGetGlyphsForCharacters(font, &unichars, &glyphs, unichars.count)
if gotGlyphs {
let cgpath = CTFontCreatePathForGlyph(font, glyphs[0], nil)
path = UIBezierPath(CGPath: cgpath!)
}
(在這種情況下,「J」)創建了一個從性格bezierpath。 Get path to trace out a character in an iOS UIFont
然後我創建一個CAShapeLayer()
並添加一個動畫。
let layer = CAShapeLayer()
layer.position = //CGPoint
layer.bounds = //CGRect()
view.layer.addSublayer(layer)
layer.path = path.CGPath
layer.lineWidth = 5.0
layer.strokeColor = UIColor.blackColor().CGColor
layer.fillColor = UIColor.clearColor().CGColor
layer.geometryFlipped = true
layer.strokeStart = 0.0
layer.strokeEnd = 1.0
let anim = CABasicAnimation(keyPath: "strokeEnd")
anim.duration = 8.0
anim.fromValue = 0.0
anim.toValue = 1.0
layer.addAnimation(anim, forKey: nil)
結果是我選擇的字符被正確地設置爲動畫。但是,當我添加另一條路徑path
.appendPath()
附加路徑被添加到原來的路徑正如你所期望的。如果我想繪製所有字符都有適當間距的字母,我該怎麼辦? 謝謝你的時間。