50行代碼實(shí)現(xiàn)刮獎(jiǎng)效果

發(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)
        
        //涂層一般也不會(huì)換顏色 就不暴露出去了 自定義在這改就行
        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
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,323評(píng)論 25 708
  • 推薦游戲:弓箭手大作戰(zhàn) 新手上線(xiàn),認(rèn)為這個(gè)游戲最大的特點(diǎn)是沒(méi)網(wǎng)也能玩! 就是沒(méi)有升星比賽,一天只能得到800金幣獎(jiǎng)...
    暗黑豬閱讀 289評(píng)論 1 0
  • 打開(kāi)舊版本的cocoa pods項(xiàng)目,報(bào)錯(cuò)解決的方案iOS "The sandbox is not in sync...
    Louis_hey閱讀 356評(píng)論 0 0
  • 首先,讓我們回顧一下你是如何用javascript來(lái)轉(zhuǎn)換一個(gè)列表。在下面的代碼中,我們用map函數(shù)來(lái)使得一個(gè)數(shù)字?jǐn)?shù)...
    編碼的哲哲閱讀 926評(píng)論 0 1

友情鏈接更多精彩內(nèi)容