(Swift+iOS)字符串轨迹转贝塞尔曲线,实现动态写字效果

(Swift+iOS)字符串轨迹转贝塞尔曲线,实现动态写字效果,第1张

概述前两天下载了一个Swift的HUD提示效果,偶然发现其中的提示效果上面的文字是动态书写的。感觉挺不错的,代码地址如下: http://code.cocoachina.com/view/129442。 然后自己使用里面部分技术写了一个OC版的, Demo地址:https://github.com/longitachi/WritingEffect/tree/master 效果图: Swift具体实现(

前两天下载了一个Swift的HUD提示效果,偶然发现其中的提示效果上面的文字是动态书写的。感觉挺不错的,代码地址如下:

http://code.cocoachina.com/vIEw/129442。

然后自己使用里面部分技术写了一个OC版的,

Demo地址:https://github.com/longitachi/WritingEffect/tree/master

效果图:



Swift具体实现(仅仅贴出Nsstring转UIBezIErPath的方法,动画简单自己加,或者下载源码):

class func bezIErPathFrom(string:String) -> UIBezIErPath{                let paths = CGPathCreateMutable()        let Fontname = __CFStringMakeConstantString("SnellRoundhand")        let FontRef:AnyObject = CTFontCreateWithname(Fontname,18,nil)                let attrString = NSAttributedString(string: string,attributes: [kCTFontAttributename as String : FontRef])        let line = CTlineCreateWithAttributedString(attrString as CFAttributedString)        let runA = CTlineGetGlyphRuns(line)                        for (var runIndex = 0; runIndex < CFArrayGetCount(runA); runIndex++){            let run = CFArrayGetValueAtIndex(runA,runIndex);            let runb = unsafeBitCast(run,CTRun.self)                        let  CTFontname = unsafeBitCast(kCTFontAttributename,UnsafePointer<VoID>.self)                        let runFontC = CFDictionaryGetValue(CTRunGetAttributes(runb),CTFontname)            let runFontS = unsafeBitCast(runFontC,CTFont.self)                        let wIDth = UIScreen.mainScreen().bounds.wIDth                        var temp = 0            var offset:CGfloat = 0.0                        for(var i = 0; i < CTRunGetGlyphCount(runb); i++){                let range = CFRangeMake(i,1)                let glyph:UnsafeMutablePointer<CGGlyph> = UnsafeMutablePointer<CGGlyph>.alloc(1)                glyph.initialize(0)                let position:UnsafeMutablePointer<CGPoint> = UnsafeMutablePointer<CGPoint>.alloc(1)                position.initialize(CGPointZero)                CTRunGetGlyphs(runb,range,glyph)                CTRunGetpositions(runb,position);                                let temp3 = CGfloat(position.memory.x)                let temp2 = (Int) (temp3 / wIDth)                let temp1 = 0                if(temp2 > temp1){                                        temp = temp2                    offset = position.memory.x - (CGfloat(temp) * wIDth)                }                let path = CTFontCreatePathForGlyph(runFontS,glyph.memory,nil)                let x = position.memory.x - (CGfloat(temp) * wIDth) - offset                let y = position.memory.y - (CGfloat(temp) * 80)                var transform = CGAffinetransformMakeTranslation(x,y)                CGPathAddpath(paths,&transform,path)                glyph.destroy()                glyph.dealloc(1)                position.destroy()                position.dealloc(1)            }                    }                let bezIErPath = UIBezIErPath()        bezIErPath.movetoPoint(CGPointZero)        bezIErPath.appendpath(UIBezIErPath(CGPath: paths))                return bezIErPath    }

根据Swift翻译的OC版本,实现如下:
- (UIBezIErPath *)transformToBezIErPath:(Nsstring *)string{    CGMutablePathref paths = CGPathCreateMutable();    CFStringRef FontnameRef = CFSTR("SnellRoundhand");    CTFontRef FontRef = CTFontCreateWithname(FontnameRef,nil);        NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:string attributes:@{(__brIDge Nsstring *)kCTFontAttributename: (__brIDge UIFont *)FontRef}];    CTlineRef lineRef = CTlineCreateWithAttributedString((CFAttributedStringRef)attrString);    CFArrayRef runArrRef = CTlineGetGlyphRuns(lineRef);        for (int runIndex = 0; runIndex < CFArrayGetCount(runArrRef); runIndex++) {        const voID *run = CFArrayGetValueAtIndex(runArrRef,runIndex);        CTRunRef runb = (CTRunRef)run;                const voID *CTFontname = kCTFontAttributename;                const voID *runFontC = CFDictionaryGetValue(CTRunGetAttributes(runb),CTFontname);        CTFontRef runFontS = (CTFontRef)runFontC;                CGfloat wIDth = [UIScreen mainScreen].bounds.size.wIDth;                int temp = 0;        CGfloat offset = .0;                for (int i = 0; i < CTRunGetGlyphCount(runb); i++) {            CFRange range = CFRangeMake(i,1);            CGGlyph glyph = 0;            CTRunGetGlyphs(runb,&glyph);            CGPoint position = CGPointZero;            CTRunGetpositions(runb,&position);                        CGfloat temp3 = position.x;            int temp2 = (int)temp3/wIDth;            CGfloat temp1 = 0;                        if (temp2 > temp1) {                temp = temp2;                offset = position.x - (CGfloat)temp;            }                        CGPathref path = CTFontCreatePathForGlyph(runFontS,glyph,nil);            CGfloat x = position.x - (CGfloat)temP*wIDth - offset;            CGfloat y = position.y - (CGfloat)temp * 80;            CGAffinetransform transform = CGAffinetransformMakeTranslation(x,y);            CGPathAddpath(paths,path);                        CGPathRelease(path);        }        CFRelease(runb);        CFRelease(runFontS);    }        UIBezIErPath *bezIErPath = [UIBezIErPath bezIErPath];    [bezIErPath movetoPoint:CGPointZero];    [bezIErPath appendpath:[UIBezIErPath bezIErPathWithCGPath:paths]];        CGPathRelease(paths);    CFRelease(FontnameRef);    CFRelease(FontRef);        return bezIErPath;}
总结

以上是内存溢出为你收集整理的(Swift+iOS)字符串轨迹转贝塞尔曲线,实现动态写字效果全部内容,希望文章能够帮你解决(Swift+iOS)字符串轨迹转贝塞尔曲线,实现动态写字效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1079556.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存