Swift4.0仿微信底部彈框

最近用Swift4.0編寫了一個(gè)微信底部彈框,雖說(shuō)沒有什么大的技術(shù)含量,但通過(guò)這么一寫措嵌,發(fā)現(xiàn)了很多的問題。語(yǔ)法改動(dòng)較Swift3.0還是有那么一丟丟的芦缰。要追的知識(shí)點(diǎn)還是有些滴企巢!

通過(guò)一個(gè)小小的例子來(lái)重新熟悉了一下這些語(yǔ)法,因?yàn)楹芫脹]有用Swift編寫項(xiàng)目了让蕾,之前還是Swift2.0的時(shí)候?qū)戇^(guò)一個(gè)小項(xiàng)目浪规,當(dāng)然也是高仿人家的項(xiàng)目頁(yè)面。后期會(huì)用Swift4.0重寫以前的那些小項(xiàng)目探孝。盡情期待笋婿。。顿颅。

廢話不多說(shuō)缸濒,直接上代碼。粱腻。庇配。

//
//  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? = "我是一個(gè)標(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: 點(diǎn)擊屏幕彈出退出
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

用法:
點(diǎn)擊一個(gè)button, 彈窗底部效果

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

以上就是封裝的一個(gè)仿微信底部彈框的例子啸澡。
GitHub地址:https://github.com/soliloquy-local/WeChatActionSheet
寫得不好希望大家指正,謝謝5省6亡!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末揪漩,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子吏口,更是在濱河造成了極大的恐慌奄容,老刑警劉巖冰更,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異昂勒,居然都是意外死亡蜀细,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門戈盈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)奠衔,“玉大人,你說(shuō)我怎么就攤上這事塘娶」榻铮” “怎么了?”我有些...
    開封第一講書人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵刁岸,是天一觀的道長(zhǎng)脏里。 經(jīng)常有香客問我,道長(zhǎng)虹曙,這世上最難降的妖魔是什么迫横? 我笑而不...
    開封第一講書人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮酝碳,結(jié)果婚禮上矾踱,老公的妹妹穿的比我還像新娘。我一直安慰自己疏哗,他們只是感情好呛讲,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著沃斤,像睡著了一般圣蝎。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上衡瓶,一...
    開封第一講書人閱讀 51,182評(píng)論 1 299
  • 那天坐搔,我揣著相機(jī)與錄音通熄,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛脯颜,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播避乏,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼探橱,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了蛮放?” 一聲冷哼從身側(cè)響起缩抡,我...
    開封第一講書人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎包颁,沒想到半個(gè)月后瞻想,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體压真,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年蘑险,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了滴肿。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡佃迄,死狀恐怖泼差,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情呵俏,我是刑警寧澤堆缘,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站柴信,受9級(jí)特大地震影響套啤,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜随常,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一潜沦、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧绪氛,春花似錦唆鸡、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至序目,卻和暖如春臂痕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背猿涨。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工握童, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人叛赚。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓澡绩,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親俺附。 傳聞我的和親對(duì)象是個(gè)殘疾皇子肥卡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,077評(píng)論 25 707
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件事镣、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,094評(píng)論 4 62
  • Lucia老師是在我成長(zhǎng)路上重要的一個(gè)人步鉴。那個(gè)時(shí)候,我臨近畢業(yè),對(duì)人生的道路唠叛、選擇還不明確只嚣,想搞清楚我的人生概況走...
    Stormy閱讀 241評(píng)論 0 0
  • 身體越來(lái)越敏感了!人多嘴雜的地方特別耗能量艺沼!不喜歡和喋喋不休的人在一起了!連自己的嘴巴都管不住的人蕴掏,定力可想而知了...
    竺子閱讀 200評(píng)論 0 0
  • #每日500字#第17天 最近這段時(shí)間一直在簡(jiǎn)書更新內(nèi)容障般,日常隨手寫點(diǎn),即刻發(fā)布盛杰,不關(guān)注評(píng)價(jià)挽荡,不關(guān)注點(diǎn)贊,整個(gè)就是...
    輕松愉快的心聲閱讀 605評(píng)論 6 3