Swift4.0仿微信底部彈框

最近用Swift4.0編寫了一個微信底部彈框,雖說沒有什么大的技術(shù)含量,但通過這么一寫,發(fā)現(xiàn)了很多的問題。語法改動較Swift3.0還是有那么一丟丟的。要追的知識點還是有些滴!

通過一個小小的例子來重新熟悉了一下這些語法,因為很久沒有用Swift編寫項目了,之前還是Swift2.0的時候?qū)戇^一個小項目,當(dāng)然也是高仿人家的項目頁面。后期會用Swift4.0重寫以前的那些小項目。盡情期待。。。

廢話不多說,直接上代碼。。。

//
//  ActionSheetViewController.swift
//  WeChatActionSheet
//
//  Created by soliloquy on 2017/10/12.
//  Copyright ? 2017年 soliloquy. All rights reserved.
//

import UIKit

public let kScreenWidth: CGFloat = UIScreen.main.bounds.size.width
public let kScreenHeight: CGFloat = UIScreen.main.bounds.size.height

class ActionSheetViewController: UIViewController {
    
    // ----------------外界調(diào)用的屬性值--------------
    var valueBlock: valueBlock?
    /// cell內(nèi)容
    var cellTitleList = [String]()
    /// cell字體顏色
    var cellTitleColor = UIColor.green
    /// cell字體大小
    var cellTitleFont: CGFloat = 17
    /// 標(biāo)題
    var titleString: String? = "我是一個標(biāo)題"
    /// 標(biāo)題字體大小
    var titleFont: CGFloat = 15
    
    
    // ---------------內(nèi)部屬性-------------------
    typealias valueBlock = (NSInteger)->Swift.Void
    fileprivate var tableView = UITableView()
    fileprivate var overVeiw = UIView()
    fileprivate let cellID = "cellID"
    fileprivate var tableViewHight: CGFloat {
        return CGFloat(cellTitleList.count) * rowHight + headerViewHight + footerViewHight
    }
    fileprivate let rowHight: CGFloat = 50
    fileprivate let headerViewHight: CGFloat = 50
    fileprivate var footerViewLineHight: CGFloat = 5
    fileprivate var footerViewHight: CGFloat {
        return headerViewHight + footerViewLineHight
    }
    
    required init?(cellTitleList: [String]!) {
        super.init(nibName: nil, bundle: nil)
        // 初始化
        self.cellTitleList = cellTitleList;
        
        view.backgroundColor = UIColor.clear
        self.providesPresentationContextTransitionStyle = true
        self.definesPresentationContext = true
        self.modalPresentationStyle = .custom
        
        // 初始化UI
        setupUIViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        UIView.animate(withDuration: 0.25) {
            var frame = self.tableView.frame
            frame.origin.y = kScreenHeight-self.tableViewHight
            self.tableView.frame = frame
            self.overVeiw.alpha = 0.5
        }
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
}

// MARK: 初始化UI
extension ActionSheetViewController {

    func setupUIViews() {
        overVeiw =
            UIView(frame: CGRect(x: 0, y: 0, width:kScreenWidth, height: kScreenHeight))
        overVeiw.backgroundColor = UIColor.black
        overVeiw.alpha = 0.0
        view.addSubview(overVeiw)
        tableView = UITableView(frame: CGRect(x: 0, y: kScreenHeight, width: kScreenWidth, height: tableViewHight), style: .plain)
        overVeiw.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.isScrollEnabled = false
        tableView.bounces = false
        tableView.isPagingEnabled = false
        tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        tableView.register(TitleCell.classForCoder(), forCellReuseIdentifier: cellID)
    }
}

// MARK: 點擊屏幕彈出退出
extension ActionSheetViewController {
    
    func sheetViewDismiss() {
        UIView.animate(withDuration: 0.25, animations: {
            var frame = self.tableView.frame
            frame.origin.y = kScreenHeight
            self.tableView.frame = frame
            self.overVeiw.alpha = 0
            
        }) { (_) in
            self.dismiss(animated: false, completion: nil)
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        sheetViewDismiss()
    }
    
    @objc func cancelBtnDidClick(btn: UIButton) {
        sheetViewDismiss()
    }
}

// MARK: UITableViewDelegate, UITableViewDataSource
extension ActionSheetViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellTitleList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! TitleCell
        cell.titleLabel.text = cellTitleList[indexPath.row]
        cell.titleLabel.textColor = cellTitleColor
        cell.titleLabel.font = UIFont.systemFont(ofSize: cellTitleFont)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return CGFloat(rowHight)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        if (self.valueBlock != nil) {
            self.valueBlock!(indexPath.row)
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return headerViewHight
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return headerViewHight + 5
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let view = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: headerViewHight))
        // 標(biāo)題
        let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: headerViewHight))
        titleLabel.text = titleString
        titleLabel.font = UIFont.systemFont(ofSize: titleFont)
        titleLabel.textColor = UIColor.lightGray
        titleLabel.textAlignment = .center
        view.addSubview(titleLabel)
        // 線
        let lineView = UIView(frame: CGRect(x: 0, y: view.frame.size.height-1, width: kScreenWidth, height: 1))
        lineView.backgroundColor = UIColor(red: 220/250.0, green: 220/250.0, blue: 220/250.0, alpha: 1.0)
        view.addSubview(lineView)
        return view
        
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: footerViewHight))
        
        let lineView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: footerViewLineHight))
        lineView.backgroundColor = UIColor.lightGray
        footerView.addSubview(lineView)
        
        let btn = UIButton(type: .custom)
        btn.frame = CGRect(x: 0, y: footerViewLineHight, width: kScreenWidth, height: footerViewHight-footerViewLineHight)
        footerView.addSubview(btn)
        btn.setTitle("取消", for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: titleFont)
        btn.setTitleColor(UIColor.black, for: .normal)
        btn.addTarget(self, action: #selector(cancelBtnDidClick(btn:)), for: .touchUpInside)
        
        return footerView;
    }
}


//
//  TitleCell.swift
//  WeChatActionSheet
//
//  Created by soliloquy on 2017/10/13.
//  Copyright ? 2017年 soliloquy. All rights reserved.
//

import UIKit

class TitleCell: UITableViewCell {

    let titleLabel = UILabel()
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
       
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
       super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        titleLabel.textAlignment = .center
        addSubview(titleLabel)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        titleLabel.frame = self.bounds
    }

}

先看效果。。。。

GIF.gif

用法:
點擊一個button, 彈窗底部效果

@IBAction func didClick(_ sender: UIButton) {
    
    let acVC = ActionSheetViewController(cellTitleList: ["保存", "收藏", "分享", "點贊"])!
    acVC.valueBlock = { index in
        print(index)
    }
    acVC.cellTitleColor = UIColor.red
    acVC.cellTitleFont = 17
    acVC.titleString = "當(dāng)你彈出來時,我love你"
    
    present(acVC, animated: false, completion:  nil)
}

以上就是封裝的一個仿微信底部彈框的例子。
GitHub地址:https://github.com/soliloquy-local/WeChatActionSheet
寫得不好希望大家指正,謝謝!??!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,039評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,318評論 4 61
  • Lucia老師是在我成長路上重要的一個人。那個時候,我臨近畢業(yè),對人生的道路、選擇還不明確,想搞清楚我的人生概況走...
    Stormy閱讀 262評論 0 0
  • 身體越來越敏感了!人多嘴雜的地方特別耗能量!不喜歡和喋喋不休的人在一起了!連自己的嘴巴都管不住的人,定力可想而知了...
    竺子閱讀 229評論 0 0
  • #每日500字#第17天 最近這段時間一直在簡書更新內(nèi)容,日常隨手寫點,即刻發(fā)布,不關(guān)注評價,不關(guān)注點贊,整個就是...
    輕松愉快的心聲閱讀 737評論 6 3

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