【发布时间】:2016-06-23 02:01:33
【问题描述】:
我正在制作一个用户可以用手指画线的游戏。网站上有很多方法。我尝试了两种方法,一种在 UIView (UIKit) 中使用 CGContext,另一种在 SpriteKit 中使用 CGPath 和 SKShapeNode。但后者显示出更好的质量。第一个使用 CGContext 有丑陋的粗糙边缘。
请查看以下屏幕截图。我还在此处附上了这两种方法的部分代码(在touchesMove 函数中)。
注意:var ref = CGPathCreateMutable()
CGPathAddLineToPoint(ref, nil, currentPoint.x, currentPoint.y)
UIGraphicsBeginImageContext(self.frame.size)
let context = UIGraphicsGetCurrentContext()
tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
CGContextAddPath(context, ref)
// 3
CGContextSetLineCap(context, CGLineCap.Round)
CGContextSetLineWidth(context, brushWidth)
CGContextSetRGBStrokeColor(context, red, green, blue, 1.0)
CGContextSetBlendMode(context, CGBlendMode.Normal)
// 4
CGContextStrokePath(context)
// 5
UIGraphicsEndImageContext()
SpriteKit 中的 SKShapeNode
注:var lineDrawing = SKShapeNode()
CGPathAddLineToPoint(ref, nil, location.x, location.y)
lineDrawing.path = ref
lineDrawing.lineWidth = 3
lineDrawing.strokeColor = UIColor.blackColor()
lineDrawing.alpha = 1.0
self.addChild(lineDrawing)
如何在 UIView 中画出与 SKShapeNode 相同质量的线条?
【问题讨论】:
-
打印实际路径。我的猜测是每条路径都有不同数量的路径。
标签: ios swift uiview sprite-kit cgcontext