//
|
// MineInfoView.swift
|
// OKProject
|
//
|
// Created by alvin_y on 2020/6/13.
|
// Copyright © 2020 yangwang. All rights reserved.
|
//
|
|
import UIKit
|
|
typealias MineInfoBlock = (Int) -> ()
|
|
class MineInfoView: UIView {
|
|
/// UIPickerView
|
@IBOutlet weak var pickerView: UIPickerView!
|
|
/// 取消
|
@IBOutlet weak var button_cancel: UIButton!
|
|
/// 提交
|
@IBOutlet weak var button_submit: UIButton!
|
|
/// 容器
|
@IBOutlet weak var view_container: YYView!
|
|
/// 数据源
|
private var dataSource: [String] = []
|
|
/// 成功
|
var mineInBlock: MineInfoBlock?
|
|
/// 获取Self
|
/// - Returns: Self
|
class func instance(data: [String]) -> MineInfoView {
|
let v = UINib(nibName: "MineInfoView", bundle: nil).instantiate(withOwner: self, options: nil).first as! MineInfoView
|
v.setupViews()
|
v.bindRx()
|
v.dataSource = data
|
return v
|
}
|
|
//MARK: - UI
|
func setupViews() {
|
self.frame = UIScreen.main.bounds
|
self.view_container.transform = CGAffineTransform.init(translationX: 0, y: screenH)
|
self.backgroundColor = UIColor.color(light: UIColor.color(hexString: "#000000",0.5), dark: UIColor.color(hexString: "#000000",0.61))
|
|
pickerView.delegate = self
|
pickerView.dataSource = self
|
pickerView.reloadAllComponents()
|
}
|
|
|
//MARK: - Rx
|
func bindRx() {
|
button_submit.rx.tap.subscribe(onNext: {[unowned self] (_) in
|
let row = self.pickerView.selectedRow(inComponent: 0)
|
self.dismiss()
|
self.mineInBlock?(row)
|
}).disposed(by: rx.disposeBag)
|
|
button_cancel.rx.tap.subscribe(onNext: {[unowned self] (_) in
|
self.dismiss()
|
}).disposed(by: rx.disposeBag)
|
}
|
|
/// 显示
|
func show(success: @escaping (Int) -> ()) {
|
self.mineInBlock = success
|
app.window?.addSubview(self)
|
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: screenH)
|
}) { (_) in
|
self.removeFromSuperview()
|
}
|
|
}
|
|
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
|
var point: CGPoint? = touches.first?.location(in: self)
|
point = view_container.layer.convert(point ?? CGPoint.zero, from: self.layer)
|
if view_container.layer.contains(point ?? CGPoint.zero) {
|
//处理点击到这个view中要执行的事件
|
return
|
}
|
super.touchesBegan(touches, with: event)
|
self.dismiss()
|
}
|
}
|
// MARK: - UIPickerViewDelegate
|
extension MineInfoView:UIPickerViewDelegate,UIPickerViewDataSource{
|
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
|
return dataSource.count
|
}
|
|
func numberOfComponents(in pickerView: UIPickerView) -> Int {
|
return 1
|
}
|
|
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
|
//时间表的线
|
for singleLine in pickerView.subviews {
|
if singleLine.frame.size.height < 2{
|
singleLine.isHidden = true
|
}
|
}
|
|
let label:UILabel = view as? UILabel ?? UILabel()
|
label.font = UIFont.boldSystemFont(ofSize: 16)
|
label.textAlignment = .center
|
label.adjustsFontSizeToFitWidth = true
|
label.text = dataSource[row]
|
return label
|
}
|
|
|
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
|
return screenW - 28
|
}
|
|
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
|
return 25
|
}
|
|
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
|
}
|
}
|