我正在开发一款应用程序,允许用户使用手指作为蜡笔在绘画中填色。有人可以帮我制造蜡笔效果吗?谢谢。
我正在开发一款应用程序,允许用户使用手指作为蜡笔在绘画中填色。有人可以帮我制造蜡笔效果吗?谢谢。
试试这个
class myView {
var crayon = UIColor(patternImage: UIImage(named: "acrayontexture")!)
重写 func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
firstTouch = (touches.first?.location(in: self))!
aPath.move(to: firstTouch)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if firstTouch != lastTouch {
lastTouch = (touches.first?.location(in: self))! ;
aPath.addLine(to: lastTouch)
setNeedsDisplay()
}
override func draw(_ rect: CGRect) {
let context = UIGraphicsGetCurrentContext() ;
context?.addPath(aPath)
context?.setLineWidth(10.0)
context?.setStrokeColor(crayon.cgColor) ;
context?.setLineCap(.round)
context?.setShadow(offset: CGSize(width: 5.0 , height: 4.0), blur: 4.0) ;
//
context?.strokePath()
}
}
}
`
您可以参考下面的链接开始。在这里,您可以按照教程中给出的RGB值更改笔或蜡笔的颜色。
http://www.ifans.com/forums/showthread.php?t=132024
您可以在屏幕上提供表示不同颜色的按钮,并根据点击的按钮设置当前笔的RGB值,然后使用手工填充颜色。在drawRect中使用的标准Quartz调用实际上不会给您提供您正在寻找的蜡笔效果,也不会使用任何默认行为。
最简单的实现方法是创建一系列透明的蜡笔纹理,至少在刚开始时可以让您更好地理解。Photoshop、Fireworks和许多其他绘图应用程序已经具有蜡笔效果的画笔和线条样式,因此您可以在透明背景上尝试几个样式以获得可用的“斑点”(小圆形蜡笔外观的透明PNG)。
然后,当用户在设备上拖动手指时,记录路径并沿着路径绘制这些“斑点”图像。如果人们来回画,就会放下更多的颜色,从而获得更深的(更实心/不透明)颜色。
这是解决此问题的一种非常粗略但常见的方法。您可以使用遮罩、图案、渐变和其他Quartz方法变得非常聪明,但是要交付您想要的内容的复杂性可能会超过图像方法的简单性。
以下是一些建议,以获得最佳效果: