Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了将String转换为其表示的路径画到屏幕上大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

关于这个问题,我已经在另一篇blog中有所提及: CoreText精彩文字轮廓绘制动画的一点改进 不过原有的转换代码使用Obj-C写的,在这里我们尝试将其转换为Swift语言,然后利用它实现一个测试小程序. 首先贴出原来Objc的代码: - (void) setupTextLayer { if (self.pathLayer != nil) { [self.penLayer

关于这个问题,我已经在另一篇blog中有所提及:

@L_489_0@

不过原有的转换代码使用Obj-C写的,在这里我们尝试将其转换为Swift语言,然后利用它实现一个测试小程序.

首先贴出原来Objc的代码:

@H_674_23@- (void) setupTextLayer { if (self@H_301_16@.pathLayer != nil) { [self@H_301_16@.penLayer removeFromSuperlayer]; [self@H_301_16@.pathLayer removeFromSuperlayer]; self@H_301_16@.pathLayer = nil; self@H_301_16@.penLayer = nil; } // Create path from text // See: http://www.codeproject.com/KB/iPhone/Glyph.aspx // License: The Code Project Open License (CPOL) 1.02 http://www.codeproject.com/info/cpol10.aspx CGMutablePathRef letters = CGPathCreateMutable(); CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica-Bold"),72.0f,NULL); NSDictionary *attrs = [NSDictionary DictionaryWithObjectsAndKeys: (id)font,kCTFontAttributename,nil]; NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"你好,大熊猫侯佩!" //NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"Hello World!" attributes:attrs]; CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attrString); CFArrayRef runArray = CTLineGetGlyphRuns(linE); // for each RUN for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++) { // Get FONT for this run CTRunRef run = (CTRunRef)CFArrayGetValueATindex(runArray,runIndeX); CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run),kCTFontAttributeName); // for each GLYPH in run for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++) { // get Glyph & Glyph-data CFRange thisGlyphRange = CFRangeMake(runGlyphIndex,1); CGGlyph glyph; CGPoint position; CTRunGetGlyphs(run,thisGlyphRange,&glyph); CTRunGetPositions(run,&position); // Get PATH of outline { CGPathRef letter = CTFontCreatePathForGlyph(runFont,glyph,NULL); CGAffineTransform t = CGAffineTransformMakeTranslation(position@H_301_16@.x,position@H_301_16@.y); CGPathAddPath(letters,&t,letter); CGPathRelease(letter); } } } CFRelease(linE); UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointZero]; [path appendPath:[UIBezierPath bezierPathWithCGPath:letters]]; CGPathRelease(letters); CFRelease(font); CAShapeLayer *pathLayer = [CAShapeLayer layer]; pathLayer@H_301_16@.frame = self@H_301_16@.animationLayer@H_301_16@.bounds; pathLayer@H_301_16@.bounds = CGPathGetBoundingBox(path@H_301_16@.CGPath); //pathLayer.BACkgroundColor = [[UIColor yellowColor] CGColor]; pathLayer@H_301_16@.geometryFlipped = YES; pathLayer@H_301_16@.path = path@H_301_16@.CGPath; pathLayer@H_301_16@.strokeColor = [[UIColor blackColor] CGColor]; pathLayer@H_301_16@.fillColor = nil; pathLayer@H_301_16@.lineWidth = 5.0f; //pathLayer.lineJoin = kCALineJoinBevel; pathLayer@H_301_16@.lineJoin = kCALineJoinMiter; [self@H_301_16@.animationLayer addSublayer:pathLayer]; self@H_301_16@.pathLayer = pathLayer; //happy commit NSLog(@"Hello World!!!"); //UIImage *penImage = [UIImage imagenamed:@"noun_project_347_2.png"]; UIImage *penImage = [UIImage imagenamed:@"bee.png"]; CALayer *penLayer = [CALayer layer]; penLayer@H_301_16@.contents = (id)penImage@H_301_16@.CGImage; penLayer@H_301_16@.anchorPoint = CGPointZero; penLayer@H_301_16@.frame = CGRectMake(0.0f,0.0f,penImage@H_301_16@.size@H_301_16@.width/5,penImage@H_301_16@.size@H_301_16@.height/5); [pathLayer addSublayer:penLayer]; self@H_301_16@.penLayer = penLayer; }

看起来颇长啊!不过不要太在意,因为我们要用Swift重写的代码提取其中中间的一部分,这样可以更好的重用.

新建一个项目,基于Swift语言.

项目中新建一个Swift源代码文件,该文件扩展了String类,我们在其扩展中先写一个帮助方法的存根:

@H_674_23@extension String{ func toPath(font:CTFont)->CGPath{ } }

toPath方法用来实现任意String实例到CGPath路径的转换,在其中添加如下内容:

@H_674_23@let letters:CGMutablePathRef = CGPathCreateMutable() let attrs = [kCTFontAttributename as String:font] let attrString:NSAttributedString = NSAttributedString(String: self,attributes: attrs) let line:CTLine = CTLineCreateWithAttributedString(attrString) let runArray = CTLineGetGlyphRuns(linE) for runIndex in 0..<CFArrayGetCount(runArray){ let run = CFArrayGetValueATindex(runArray,runIndeX) let runb = unsafeBitCast(run,CTRun.self) //let runFont:CTFont = CFDictionaryGetValue(CTRunGetAttributes(runb),kCTFontAttributename as String) as! CTFont let CTFontName = unsafeBitCast(kCTFontAttributename,UnsafePointer<Void>.self) let runFontC = CFDictionaryGetValue(CTRunGetAttributes(runb),CTFontName) let runFont = unsafeBitCast(runFontC,CTFont.self) //for each GLYPH in run for runGlyphIndex in 0..<CTRunGetGlyphCount(runb){ //get Glyph & Glyph-data let glyphRange = CFRange(LOCATIOn: runGlyphIndex,length: 1) //let glyph:UnsafeMutablePointer<CGGlyph> = UnsafeMutablePointer<CGGlyph>.alloc(1) //glyph.initialize(0) var glyph:CGGlyph = 0 let position:UnsafeMutablePointer<CGPoint> = UnsafeMutablePointer<CGPoint>.alloc(1) position.initialize(CGPoint.zero) CTRunGetGlyphs(runb,glyphRange,&glyph) CTRunGetPositions(runb,position) //Get PATH of outline //let letter = CTFontCreatePathForGlyph(runFont,glyph.memory,nil) let letter = CTFontCreatePathForGlyph(runFont,nil) var t = CGAffineTransformMakeTranslation(position.memory.x,position.memory.y) //let tx:UnsafeMutablePointer<CGAffineTransform> = UnsafeMutablePointer<CGAffineTransform>.alloc(1) //tx.initialize(t) CGPathAddPath(letters,letter) //CGPathRelease(letter) position.destroy() position.dealloc(1) } } let path = UIBezierPath() path.moveToPoint(CGPoint.zero) path.appendPath(UIBezierPath(CGPath: letters)) return path.CGPath

大家可以对照原来的Obj-C版本看一下,大致都是一一对应的,只有少数几个涉及操作C语言数据的地方有修改,大家可以参我写的另一篇blog:

Swift中如何转换不同类型的Mutable指针

核心功能有了,下面就好办了!我们想要的是点击屏幕开始显示动画,于是重载如下方法:

@H_674_23@override func touchesEnded(touches: Set<UITouch>,withEvent event: UIEvent?) { if isAnimaTing { return } isAnimaTing = true flyerLayer.opacity = 0.8 pathLayer.removeAllAnimations() flyerLayer.removeAllAnimations() let strokeAnimation = CABasicAnimation(keyPath: "strokeEnd") strokeAnimation.duration = 20.0 strokeAnimation.fromValue = 0.0 strokeAnimation.toValue = 1.0 strokeAnimation.delegate = self pathLayer.addAnimation(strokeAnimation,forKey: nil) let flyAnimation = CAKeyframeAnimation(keyPath: "position") flyAnimation.duration = 20.0 flyAnimation.path = pathLayer.path flyAnimation.calculationMode = kCAAnimationPaced flyerLayer.addAnimation(flyAnimation,forKey: nil) }

下面是App实际运行的效果:

大佬总结

以上是大佬教程为你收集整理的将String转换为其表示的路径画到屏幕上全部内容,希望文章能够帮你解决将String转换为其表示的路径画到屏幕上所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。