由文字生成path后制作写字的动画
来源:互联网 发布:java经典小程序实例 编辑:程序博客网 时间:2024/04/28 17:34
在看下面这个开源组件的时候,发现一个很棒的方法,可以将文字生成path,这样就可以作出用笔写字的效果了。
https://github.com/MP0w/MPParallaxCollection
关键代码:
-(CGPathRef)pathRefFromText
{
NSAttributedString *attributed = self.attributedText;
CGMutablePathRef letters = CGPathCreateMutable();
CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)attributed);
CFArrayRef runArray = CTLineGetGlyphRuns(line);
for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++)
{
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++)
{
CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1);
CGGlyph glyph;
CGPoint position;
CTRunGetGlyphs(run, thisGlyphRange, &glyph);
CTRunGetPositions(run, thisGlyphRange, &position);
CGPathRef letter = CTFontCreatePathForGlyph(runFont, glyph, NULL);
CGAffineTransform t = CGAffineTransformMakeTranslation(position.x, position.y);
CGPathAddPath(letters, &t, letter);
CGPathRelease(letter);
}
}
UIBezierPath *path = [UIBezierPath bezierPathWithCGPath:letters];
CGRect boundingBox = CGPathGetBoundingBox(letters);
CGPathRelease(letters);
CFRelease(line);
[path applyTransform:CGAffineTransformMakeScale(1.0, -1.0)];
[path applyTransform:CGAffineTransformMakeTranslation(0.0, boundingBox.size.height)];
if (self.reversedAnimation) {
return [[path bezierPathByReversingPath] CGPath];
}
return [path CGPath];
}
{
NSAttributedString *attributed = self.attributedText;
CGMutablePathRef letters = CGPathCreateMutable();
CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)attributed);
CFArrayRef runArray = CTLineGetGlyphRuns(line);
for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++)
{
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++)
{
CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1);
CGGlyph glyph;
CGPoint position;
CTRunGetGlyphs(run, thisGlyphRange, &glyph);
CTRunGetPositions(run, thisGlyphRange, &position);
CGPathRef letter = CTFontCreatePathForGlyph(runFont, glyph, NULL);
CGAffineTransform t = CGAffineTransformMakeTranslation(position.x, position.y);
CGPathAddPath(letters, &t, letter);
CGPathRelease(letter);
}
}
UIBezierPath *path = [UIBezierPath bezierPathWithCGPath:letters];
CGRect boundingBox = CGPathGetBoundingBox(letters);
CGPathRelease(letters);
CFRelease(line);
[path applyTransform:CGAffineTransformMakeScale(1.0, -1.0)];
[path applyTransform:CGAffineTransformMakeTranslation(0.0, boundingBox.size.height)];
if (self.reversedAnimation) {
return [[path bezierPathByReversingPath] CGPath];
}
return [path CGPath];
}
0 0
- 由文字生成path后制作写字的动画
- Path动画--书写文字的动画实现
- 用delphi制作由小到大的动画窗体!
- 使用path制作各类型动画路径
- Fluent 后处理动画制作
- KOOLMOVES 文字动画制作软件 注册码
- CSS3 动态生成文字旋转动画样式
- canvas操作文字像素生成动画
- 在picturebox上画一条线,然后在线上写字,把文字对应的线删掉
- 点读笔写字App(2)——初始化本子上已经记录的文字
- CCBAnimationManager的使用:使用cocos2d-x程序控制,由cocosBuilder生成cbbi中的动画
- CCBAnimationManager的使用:使用cocos2d-x程序控制,由cocosBuilder生成cbbi中的动画
- OpenGL 动画的制作
- OpenGL 动画的制作
- 制作简单的动画
- 动画的制作
- 蝴蝶动画的制作
- 简单的动画制作
- (转载)Objective C GCD介绍
- 关于double类型的输入/输出格式 作者:Sunny_ideal
- Linux下Django的安装
- ExtJS4获取控件展示的信息---获取grid选中项信息
- 利用数学知识快速理解按位与& 按位或| 按位异或^ 按位取反~
- 由文字生成path后制作写字的动画
- [算法]有趣算法合辑[31-40]
- 动态的增加和删除表格并根据光标获取行索引
- java环境变量设置
- 教新手如何去学习php框架
- 通过ContextMenu上下文菜单获取ListView中被选中的条目item的信息
- 在 C/C++ 程序调用 Java 代码
- OCP 1Z0 052 109
- Express框架之app.js配置文件说明