// // ScanView.swift // Twear // // Created by yangbin on 2021/12/6. // import UIKit import SnapKit enum ScanViewType: Int { case ScanStart = 0 case ScanComplete case Pairing case PairSuccess case PairFail case WearTip case FailReason } class ScanView: UIView { typealias clickActionClosure = (_ index: Int, _ action: Any?) -> Void var clickClosure: clickActionClosure? var deviceArray: [ScanDevice] = [] { didSet { if type == .ScanStart { bgView.snp.updateConstraints { make in make.height.equalTo(min(75 + max(CGFloat(deviceArray.count) * 50.0, 150), SCREEN_HEIGHT/2.0)) } tableView.snp.updateConstraints { make in make.height.equalTo(min(max(CGFloat(deviceArray.count) * 50.0, 150), SCREEN_HEIGHT/2.0-75)) } tableView.reloadData() } } } var type: ScanViewType = .ScanStart private let bgView = UIView() //白色框 private var tableViewHeight: CGFloat = 0 // var private lazy var titleLabel: UILabel = { let label = UILabel() label.font = BoldFont(13) label.textColor = .black label.textAlignment = .left label.text = LocString("正在搜索...") return label }() private lazy var detailLabel: UILabel = { let label = UILabel() label.font = RegularFont(11) label.numberOfLines = 0 label.textColor = .black return label }() private lazy var cancelBtn: UIButton = { let button = UIButton() button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside) button.setTitleColor(UIColor.rgbColorFromHex(0x00993E), for: .normal) button.titleLabel?.font = BoldFont(16) button.setTitle(LocString("暂不配对"), for: .normal) button.tag = 2 return button }() private lazy var reasonBtn: UIButton = { let button = UIButton() button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside) button.setTitleColor(UIColor.rgbColorFromHex(0xD00000), for: .normal) button.titleLabel?.font = RegularFont(14) button.setTitle(LocString("为什么配对失败?"), for: .normal) button.tag = 5 return button }() private lazy var tableView: UITableView = { let tableView = UITableView(frame: CGRect(x: 0, y: 31, width: SCREEN_WIDTH-20, height: 150)) tableView.bounces = false tableView.delegate = self tableView.dataSource = self tableView.separatorStyle = .none return tableView }() private lazy var splitLine: UIView = { let view = UIView() view.backgroundColor = LineColor return view }() private lazy var imageView: UIImageView = { let view = UIImageView() return view }() private lazy var loadButton: LoaderButton = { let button = LoaderButton() button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside) button.setTitle(LocString("重新搜索"), for: .normal) button.setTitleColor(UIColor.rgbColorFromHex(0xD00000), for: .normal) button.titleLabel?.font = BoldFont(11) button.tag = 4 // button.layer.cornerRadius = 8 return button }() init() { super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)) createView() } func createView() { backgroundColor = UIColor.black.withAlphaComponent(0.1) bgView.layer.cornerRadius = 10 bgView.clipsToBounds = true bgView.backgroundColor = .white addSubview(bgView) bgView.addSubview(titleLabel) bgView.addSubview(cancelBtn) bgView.addSubview(tableView) bgView.addSubview(splitLine) bgView.addSubview(imageView) bgView.addSubview(detailLabel) bgView.addSubview(reasonBtn) bgView.addSubview(loadButton) imageView.isHidden = true detailLabel.isHidden = true reasonBtn.isHidden = true bgView.snp.makeConstraints { (make) in make.left.equalToSuperview().offset(10) make.right.equalToSuperview().offset(-10) make.bottom.equalToSuperview().offset(isiPhoneX() ? -42.5 : -22.5) make.height.equalTo(150+31+44) make.top.equalTo(titleLabel).offset(-13) } titleLabel.snp.makeConstraints { make in make.left.equalToSuperview().offset(10) make.top.equalToSuperview().offset(13) make.right.equalToSuperview().offset(-10) } loadButton.snp.makeConstraints { make in make.right.equalToSuperview() make.centerY.equalTo(titleLabel) make.width.equalTo(80) make.height.equalTo(26) } imageView.snp.makeConstraints { make in make.top.equalTo(titleLabel.snp_bottom).offset(23) make.centerX.equalToSuperview() } detailLabel.snp.makeConstraints { make in make.top.equalTo(imageView.snp_bottom).offset(25) make.left.equalToSuperview().offset(20) make.right.equalToSuperview().offset(-20) } tableView.snp.makeConstraints { make in make.left.right.equalToSuperview() make.top.equalToSuperview().offset(31) make.bottom.equalTo(splitLine.snp_top) make.height.equalTo(150) } reasonBtn.snp.makeConstraints { make in make.left.equalToSuperview().offset(20) make.bottom.equalTo(splitLine.snp_top).offset(-12) } splitLine.snp.makeConstraints { make in make.height.equalTo(0.5) make.right.equalToSuperview().offset(-10) make.left.equalToSuperview().offset(10) make.bottom.equalToSuperview().offset(-44) } cancelBtn.snp.makeConstraints { make in make.left.right.equalToSuperview() make.bottom.equalToSuperview() make.top.equalTo(splitLine.snp_bottom) } // loadButton.toggle() loadButton.cornerRadius = 1 loadButton.loaderNormalImage = UIImage(named: "微信运动") loadButton.startAnimate(loaderType: .circleRotate, loaderColor: UIColor.rgbColorFromHex(0x00993E), complete: nil) } func updateUI(_ type: ScanViewType) { splitLine.isHidden = false reasonBtn.isHidden = true loadButton.isHidden = true self.type = type switch type { case .ScanStart: bgView.snp.updateConstraints { make in make.height.equalTo(225) } cancelBtn.setTitleColor(UIColor.rgbColorFromHex(0x00993E), for: .normal) cancelBtn.setTitle(LocString("暂不配对"), for: .normal) cancelBtn.tag = 2 cancelBtn.isHidden = false titleLabel.font = BoldFont(13) titleLabel.textAlignment = .left titleLabel.text = LocString("正在搜索...") imageView.isHidden = true tableView.isHidden = false detailLabel.isHidden = true loadButton.isHidden = false loadButton.startAnimate(loaderType: .circleRotate, loaderColor: UIColor.rgbColorFromHex(0x00993E), complete: nil) case .ScanComplete: bgView.snp.updateConstraints { make in make.height.equalTo(min(75 + max(CGFloat(deviceArray.count) * 50.0, 150), SCREEN_HEIGHT/2.0)) } cancelBtn.setTitleColor(UIColor.rgbColorFromHex(0xD00000), for: .normal) cancelBtn.setTitle(LocString("暂不配对"), for: .normal) cancelBtn.tag = 2 cancelBtn.isHidden = false titleLabel.font = BoldFont(13) titleLabel.textAlignment = .left titleLabel.text = LocString("搜索完成") imageView.isHidden = true tableView.isHidden = false detailLabel.isHidden = true loadButton.isHidden = false loadButton.stopAnimate(complete: nil) case .Pairing: bgView.snp.updateConstraints { make in make.height.equalTo(220) } cancelBtn.isHidden = true titleLabel.font = BoldFont(15) titleLabel.textAlignment = .center titleLabel.text = LocString("正在配对...") imageView.isHidden = false imageView.image = UIImage(named: "pairing") tableView.isHidden = true splitLine.isHidden = true detailLabel.snp.remakeConstraints { make in make.top.equalTo(imageView.snp_bottom).offset(25) make.left.equalToSuperview().offset(20) make.right.equalToSuperview().offset(-20) } detailLabel.text = LocString("请勿操作手机和手表") detailLabel.isHidden = false case .PairSuccess: bgView.snp.updateConstraints { make in make.height.equalTo(220) } cancelBtn.setTitleColor(UIColor.rgbColorFromHex(0x00993E), for: .normal) cancelBtn.setTitle(LocString("完成"), for: .normal) cancelBtn.tag = 6 cancelBtn.isHidden = false titleLabel.font = BoldFont(15) titleLabel.textAlignment = .center titleLabel.text = LocString("配对成功") imageView.isHidden = false imageView.image = UIImage(named: "pair_success") tableView.isHidden = true detailLabel.snp.remakeConstraints { make in make.top.equalTo(imageView.snp_bottom).offset(25) make.left.equalToSuperview().offset(20) make.right.equalToSuperview().offset(-20) } detailLabel.text = LocString("恭喜你,配对成功!") detailLabel.isHidden = false case .PairFail: bgView.snp.updateConstraints { make in make.height.equalTo(250) } cancelBtn.setTitleColor(UIColor.rgbColorFromHex(0xD00000), for: .normal) cancelBtn.setTitle(LocString("重试"), for: .normal) cancelBtn.tag = 3 cancelBtn.isHidden = false titleLabel.font = BoldFont(15) titleLabel.textAlignment = .center titleLabel.text = LocString("配对失败") imageView.isHidden = false imageView.image = UIImage(named: "pair_fail") tableView.isHidden = true reasonBtn.isHidden = false detailLabel.snp.remakeConstraints { make in make.top.equalTo(imageView.snp_bottom).offset(23) make.left.equalToSuperview().offset(20) make.right.equalToSuperview().offset(-20) } detailLabel.text = LocString("配对失败,请重试") detailLabel.isHidden = false case .WearTip: bgView.snp.updateConstraints { make in make.height.equalTo(250) } cancelBtn.setTitleColor(UIColor.rgbColorFromHex(0x00993E), for: .normal) cancelBtn.setTitle(LocString("完成"), for: .normal) cancelBtn.tag = 2 cancelBtn.isHidden = false titleLabel.font = BoldFont(15) titleLabel.textAlignment = .center titleLabel.text = LocString("佩戴提示") imageView.isHidden = false imageView.image = UIImage(named: "wear_tip") tableView.isHidden = true detailLabel.snp.remakeConstraints { make in make.top.equalTo(imageView.snp_bottom).offset(20) make.left.equalToSuperview().offset(58) make.right.equalToSuperview().offset(-58) } detailLabel.text = LocString("为了准确的测量数据,和不影响APP的正常使用,请您尽量日常佩戴手表。") detailLabel.isHidden = false case .FailReason: bgView.snp.updateConstraints { make in make.height.equalTo(250) } cancelBtn.setTitleColor(UIColor.rgbColorFromHex(0x00993E), for: .normal) cancelBtn.setTitle(LocString("我知道了"), for: .normal) cancelBtn.tag = 2 cancelBtn.isHidden = false titleLabel.font = BoldFont(13) titleLabel.textAlignment = .center titleLabel.text = LocString("可能原因") imageView.isHidden = true tableView.isHidden = true detailLabel.snp.remakeConstraints { make in make.top.equalToSuperview().offset(55) make.left.equalToSuperview().offset(20) make.right.equalToSuperview().offset(-20) } detailLabel.text = LocString("1.距离太远,请将手表靠近手机;\n2.当前APP版本太低,请更新升级新版本后重试;\n3.您的手机此时存在短暂性网络通讯故障,请稍后重试;\n4.关闭蓝牙并删除当前已连接的蓝牙设备后,再次开启蓝牙重试;\n5.重启手表和手机后重试。") detailLabel.isHidden = false } } @objc private func clickBtnAction(_ sender: UIButton) { if sender.tag == 2 { dismiss() } else if sender.tag == 3 { deviceArray = [] updateUI(.ScanStart) } else if sender.tag == 4 { deviceArray = [] updateUI(.ScanStart) } else if sender.tag == 5 { updateUI(.FailReason) } else if sender.tag == 6 { updateUI(.WearTip) } if (clickClosure != nil) { clickClosure!(sender.tag, nil) } } func show() { let window = KeyWindow window.addSubview(self) self.alpha = 0 UIView.animate(withDuration: 0.25, animations: { // self.bgView.frame.origin.y = SCREEN_HEIGHT - self.tableViewHeight self.alpha = 1 }, completion: { (finish) -> Void in }) } @objc func dismiss() { UIView.animate(withDuration: 0.25, animations: { () -> Void in self.alpha = 0 }, completion: { (finish) -> Void in if finish { self.removeFromSuperview() } }) if GCDTimer.shared.isExistTimer(WithTimerName: "StartScan") { GCDTimer.shared.cancleTimer(WithTimerName: "StartScan") } if GCDTimer.shared.isExistTimer(WithTimerName: "DevicePairing") { GCDTimer.shared.cancleTimer(WithTimerName: "DevicePairing") } } deinit { print("ScanView dealloc") } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } /* // Only override draw() if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func draw(_ rect: CGRect) { // Drawing code } */ } extension ScanView: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return deviceArray.count } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 50 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCell(withIdentifier: "ScanView") if cell == nil { cell = UITableViewCell(style: .subtitle, reuseIdentifier: "ScanView") } cell?.accessoryType = .disclosureIndicator cell?.textLabel?.text = deviceArray[indexPath.row].peripheral.name ?? " " cell?.textLabel?.font = BoldFont(15) cell?.detailTextLabel?.text = "mac: \(deviceArray[indexPath.row].mac)" cell?.detailTextLabel?.font = RegularFont(11) return cell! } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath) cell?.isSelected = false if (clickClosure != nil) { clickClosure!(6, deviceArray[indexPath.row].peripheral) } } }