jsdw_ios/QuickLocation/Section/Home/Bubble/CreateBubbleDoneView.swift

99 lines
2.7 KiB
Swift

//
// CreateBubbleDoneView.swift
// QuickLocation
//
import UIKit
import RxSwift
import RxCocoa
class CreateBubbleDoneView: UIView {
var disposeBag = DisposeBag()
let heroView = BubbleHeroView()
let popupBtn = UIButton(type: .custom)
private var countdownTimer: Timer?
private var endDate: Date = Date()
private let clockImage = UIImageView()
private let timeLab = UILabel()
func startCountdown(endDate: Date) {
self.endDate = endDate
countdownTimer?.invalidate()
countdownTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
self?.updateTime()
}
updateTime()
}
func stopCountdown() {
countdownTimer?.invalidate()
countdownTimer = nil
timeLab.text = "00:00"
}
private func updateTime() {
let remaining = endDate.timeIntervalSince(Date())
if remaining <= 0 {
timeLab.text = "00:00"
countdownTimer?.invalidate()
countdownTimer = nil
return
}
let hours = Int(remaining) / 3600
let minutes = (Int(remaining) % 3600) / 60
timeLab.text = String(format: "%02d:%02d", hours, minutes)
}
private func setupUI() {
addSubview(heroView)
addSubview(clockImage)
clockImage.addSubview(timeLab)
addSubview(popupBtn)
heroView.showsCollisionDecorations = true
heroView.layoutChain
.top(33)
.leading(30)
.trailing(30)
.heightToWidth(1)
clockImage.image = UIImage(named: "Bubble/clock")
clockImage.contentMode = .scaleAspectFit
clockImage.layoutChain
.topToBottomOfView(heroView, offset: 60)
.centerX()
.width(200)
.height(184)
timeLab.font = .systemFont(ofSize: 32, weight: .heavy)
timeLab.textColor = .white
timeLab.textAlignment = .center
timeLab.layoutChain.centerX(clockImage).centerY(clockImage, offset: 28)
popupBtn.setTitle("弹出气泡", for: .normal)
popupBtn.setTitleColor(.white, for: .normal)
popupBtn.titleLabel?.font = FontManager.boboBold(18)
popupBtn.backgroundColor = UIColor(hexStr: "#16B3FF")
popupBtn.layer.cornerRadius = 16
popupBtn.layoutChain.left(20).right(20).bottom(34).height(56)
}
override init(frame: CGRect) {
super.init(frame: .zero)
backgroundColor = .white
setupUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
countdownTimer?.invalidate()
}
}