Swift 利用CGContext繪出仿微信的彈框

效果圖:

彈框.gif

自定義outputView,代碼貼上:

import UIKit

class OutputView: UIView,UITableViewDelegate,UITableViewDataSource {

    fileprivate var titleArr : [String] = []
    fileprivate var origin : CGPoint!
    fileprivate var tableview : UITableView!
    fileprivate var height : CGFloat = 40
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.white
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupTableview(_ titleArr:[String], origin:CGPoint){
        self.titleArr = titleArr
        self.origin = origin
        
        tableview = UITableView.init(frame: CGRect.init(x: origin.x, y: origin.y+10, width: 100, height: height*CGFloat(titleArr.count)), style: .plain)
        tableview.delegate = self
        tableview.dataSource = self
        tableview.separatorStyle = .singleLine
        tableview.separatorColor = UIColor.white
        tableview.separatorInset = .zero
        tableview.backgroundColor = UIColor.gray
        tableview.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cell")
        tableview.isScrollEnabled = false
        addSubview(tableview)
        
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titleArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableview.dequeueReusableCell(withIdentifier: "cell")
        cell?.textLabel?.text = titleArr[indexPath.row]
        cell?.backgroundColor = UIColor.gray
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return height
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        dimiss()
        print(titleArr[indexPath.row])
    }
    
    
    override func draw(_ rect: CGRect) {
        //拿到當(dāng)前視圖準(zhǔn)備好的畫板
        let context = UIGraphicsGetCurrentContext()
        //填充顏色
        context?.setFillColor(UIColor.gray.cgColor)
        //畫筆顏色
        context?.setStrokeColor(UIColor.gray.cgColor)
        //開始繪圖
        context?.beginPath()
        context?.move(to: CGPoint.init(x: origin.x+20, y: origin.y))
        context?.addLine(to: CGPoint.init(x: origin.x+30, y: origin.y+10))
        context?.addLine(to: CGPoint.init(x: origin.x+10, y: origin.y+10))
        context?.closePath()
        context?.drawPath(using: .fillStroke)
    }
    
    func pop(){
        UIApplication.shared.keyWindow?.addSubview(self)
        self.alpha = 0
        let frame = self.tableview.frame
        self.tableview.frame = CGRect.init(x: self.origin.x, y: self.origin.y, width: 0, height: 0)
        UIView.animate(withDuration: 0.2) { 
            self.alpha = 1
            self.tableview.frame = frame
        }
    }
    
    func dimiss(){
        UIView.animate(withDuration: 0.2, animations: { 
            self.alpha = 0
            self.tableview.frame = CGRect.init(x: self.origin.x, y: self.origin.y, width: 0, height: 0)
        }) { (ret) in
            self.removeFromSuperview()
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if !(touches.first?.view?.isEqual(self.tableview))!{
            dimiss()
        }
    }
    
}

需要使用的時(shí)候:

        let x = btn.frame.origin.x
        let y = btn.frame.origin.y
        let origin = CGPoint.init(x: x, y: y)
        let titleArr : [String] = ["掃一掃","看一看","搜一搜"]
        outputView = OutputView.init(frame: self.view.bounds)
        outputView.setupTableview(titleArr, origin: origin)
        outputView.pop()

以上

最后編輯于
?著作權(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),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,063評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,383評(píng)論 4 61
  • 如果有一天,你的努力配得上你的夢想,那么你的夢想也絕對(duì)不會(huì)辜負(fù)你的努力
    大美妞525閱讀 258評(píng)論 0 1
  • 時(shí)間如同春日枝頭綻放的花朵,悄然間讓香味盈滿了校園,不知不覺中我也已經(jīng)在大學(xué)度過了將近兩年的時(shí)光。 兩年間,課業(yè)馬...
    酒久里個(gè)丸子閱讀 244評(píng)論 0 0
  • 今天又回來到這點(diǎn),車上的旅途真是有太多的不可預(yù)見性。本來預(yù)定時(shí)間是10點(diǎn)回來的,因?yàn)槿?,一個(gè)多小時(shí)又沒了。 半夜...
    在亮閱讀 254評(píng)論 0 0

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