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) {
        //拿到当前视图准备好的画板
        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()
        }
    }
    
}

需要使用的时候:

        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()

以上

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,337评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,241评论 4 61
  • 如果有一天,你的努力配得上你的梦想,那么你的梦想也绝对不会辜负你的努力
    大美妞525阅读 239评论 0 1
  • 时间如同春日枝头绽放的花朵,悄然间让香味盈满了校园,不知不觉中我也已经在大学度过了将近两年的时光。 两年间,课业马...
    酒久里个丸子阅读 200评论 0 0
  • 今天又回来到这点,车上的旅途真是有太多的不可预见性。本来预定时间是10点回来的,因为塞车,一个多小时又没了。 半夜...
    在亮阅读 237评论 0 0