發(fā)現(xiàn)最近經(jīng)常要干舊貌換新顏的活特恬。舷暮。
涂層代碼??
class LuckyCard:CALayer
{
var point:CGPoint!{ didSet{ oldValue == nil || isFinished ? LuckyCard.drawingPath.move(to: point) : LuckyCard.drawingPath.addLine(to: point) } }
var isFinished = false { didSet { isFinished ? drawingPathArr.append(LuckyCard.drawingPath) : () } }
fileprivate lazy var drawingPathArr = [drawingPath]
fileprivate static let drawingPath:UIBezierPath =
{
let path = UIBezierPath()
path.lineWidth = 30
path.lineCapStyle = .round
return path
}()
private override init() { super.init() }
convenience init(with rect:CGRect)
{
self.init()
frame = rect
backgroundColor = UIColor.clear.cgColor
setNeedsDisplay()
}
required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override func draw(in ctx: CGContext)
{
UIGraphicsPushContext(ctx)
//涂層一般也不會換顏色 就不暴露出去了 自定義在這改就行
ctx.setFillColor(UIColor(red: 211/255, green: 211/255, blue: 211/255, alpha: 0.7).cgColor)
ctx.fill(bounds)
ctx.setBlendMode(.clear)
for path in drawingPathArr{ path.stroke() }
UIGraphicsPopContext()
}
}
extension LuckyCard
{
func handle(with point:CGPoint)
{
drawPoint = point
isFinished = false
setNeedsDisplay()
}
}
調(diào)用代碼??
fileprivate let layer = LuckyCard(with: UIScreen.main.bounds)
override func viewDidLoad()
{
super.viewDidLoad()
view.layer.addSublayer(layer)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
layer.handle(with:touches.first!.location(in: view))
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)
{
layer.handle(with:touches.first!.location(in: view))
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?)
{
layer.isFinished = true
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
{
layer.isFinished = true
}