//
|
// YYQuestionAlertController.swift
|
// FollowMeProject
|
//
|
// Created by alvin_y on 2020/9/7.
|
// Copyright © 2020 yangwang. All rights reserved.
|
//
|
|
import UIKit
|
|
typealias YYBottomOnDoneBlock = (String) -> Void
|
|
class YYBottomAlertController: YYViewController {
|
@IBOutlet weak var tableViewHeightConstraint: NSLayoutConstraint!
|
|
@IBOutlet weak var view_title: UIView!
|
@IBOutlet weak var label_title: UILabel!
|
/// UIView
|
@IBOutlet weak var view_container: UIView!
|
|
/// UITableView
|
@IBOutlet weak var tableView: UITableView!
|
|
/// 数据源
|
var dataSource: [String] = []
|
var alertTitle: String?
|
|
var onDone: YYBottomOnDoneBlock?
|
|
|
init (){
|
super.init(nibName: String(describing: YYBottomAlertController.self), bundle: Bundle.main)
|
|
modalTransitionStyle = .crossDissolve
|
modalPresentationStyle = .overFullScreen
|
|
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
|
class func display(dataSource: [String],onDone: @escaping (String) -> Void) {
|
let vc = YYBottomAlertController()
|
vc.onDone = onDone
|
vc.dataSource = dataSource
|
self.base()?.present(vc, animated: true, completion: nil)
|
}
|
|
class func display(title: String,dataSource: [String],onDone: @escaping (String) -> Void) {
|
let vc = YYBottomAlertController()
|
vc.onDone = onDone
|
vc.alertTitle = title
|
vc.dataSource = dataSource
|
self.base()?.present(vc, animated: true, completion: nil)
|
}
|
|
override func viewDidLoad() {
|
super.viewDidLoad()
|
|
// Do any additional setup after loading the view.
|
tableView.reloadData()
|
view_container.transform = CGAffineTransform.init(translationX: 0, y: view_container.maxY + 50)
|
}
|
|
override func viewWillAppear(_ animated: Bool) {
|
super.viewWillAppear(animated)
|
UIView.animate(withDuration: 0.2) {
|
self.view_container.transform = CGAffineTransform.identity
|
}
|
}
|
|
/// 隐藏
|
func dismiss() {
|
UIView.animate(withDuration: 0.2, animations: {
|
self.view_container.transform = CGAffineTransform.init(translationX: 0, y: self.view_container.maxY + 50)
|
}) { (_) in
|
self.dismiss(animated: false, completion: nil)
|
}
|
}
|
|
//MARK: - UI
|
override func setupViews() {
|
super.setupViews()
|
view.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.65)
|
|
view_container.addRoundedCorners(corners: [.topLeft,.topRight], rect: CGRect(x: 0, y: 0, width: screenW, height: CGFloat(MAXFLOAT)), radius: CGSize(width: 10, height: 10))
|
|
tableView.delegate = self
|
tableView.dataSource = self
|
tableView.separatorStyle = .none
|
tableView.register(cellWithClass: UITableViewCell.self)
|
label_title.text = alertTitle
|
view_title.isHidden = alertTitle == nil
|
}
|
|
//MARK: - Rx
|
override func bindRx() {
|
super.bindRx()
|
tableView.rx.observe(CGSize.self, #keyPath(UITableView.contentSize))
|
.subscribe(onNext: {[unowned self] (size) in
|
guard let height = size?.height else{return}
|
self.tableViewHeightConstraint.constant = height
|
})
|
.disposed(by: rx.disposeBag)
|
}
|
|
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
|
var point: CGPoint? = touches.first?.location(in: view)
|
point = view_container.layer.convert(point ?? CGPoint.zero, from: view.layer)
|
if view_container.layer.contains(point ?? CGPoint.zero) {
|
//处理点击到这个view中要执行的事件
|
return
|
}
|
super.touchesBegan(touches, with: event)
|
dismiss()
|
}
|
|
}
|
|
|
// MARK: - UITableViewDelegate
|
extension YYBottomAlertController:UITableViewDelegate{
|
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
return 54
|
}
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
tableView.deselectRow(at: indexPath, animated: true)
|
dismiss(animated: true) {
|
self.onDone?(self.dataSource[indexPath.row])
|
}
|
}
|
}
|
|
// MARK: - UITableViewDelegate
|
extension YYBottomAlertController:UITableViewDataSource{
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
return dataSource.count
|
}
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
let cell = tableView.dequeueReusableCell(withClass: UITableViewCell.self, for: indexPath)
|
cell.textLabel?.textAlignment = .center
|
cell.textLabel?.text = dataSource[indexPath.row]
|
switch dataSource[indexPath.row] {
|
case "取消":
|
cell.textLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
|
cell.textLabel?.textColor = #colorLiteral(red: 0.6901960784, green: 0.6901960784, blue: 0.6901960784, alpha: 1)
|
default:
|
cell.textLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
|
cell.textLabel?.textColor = #colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
|
}
|
return cell
|
}
|
|
}
|