나는 손가락으로 선을 그릴 수있는 게임을 만들고 있습니다. 웹 사이트에는 수많은 방법이 있습니다. UIView (UIKit)에서 CGContext를 사용하는 방법과 SpriteKit에서 CGPath 및 SKShapeNode를 사용하는 두 가지 방법을 시도했습니다. 그러나 후자는 더 나은 품질을 보여줍니다. CGContext를 사용하는 첫 번째 것은 거친 가장자리를 가지고 있습니다.iOS CGContext를 사용하는 그리기 선의 품질이 SpriteKit의 SKShapeNode보다 훨씬 나쁩니다.
다음 스크린 샷을 참조하십시오. 또한 두 가지 방법에 대한 코드 일부를 여기에 첨부했습니다 (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)
동일한 품질의 SKShapeNode로 UIView에서 선을 그리려면 어떻게해야합니까? ,
UIGraphicsBeginImageContextWithOptions(self.frame.size, false, 0)
WithOptions
버전 :
UIGraphicsBeginImageContext(self.frame.size)
당신이 사용하는 대신해야합니다
실제 경로가 인쇄되어 있습니다. 내 추측으로는 각기 다른 수의 경로가 있다는 것입니다. –