CNLiveMineExchangeViewController.swift 15 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
//
//  CNLiveMineExchangeViewController.swift
//  metaCode
//
//  Created by open on 2023/7/8.
//

import Foundation
import SnapKit

class CNLiveMineExchangeViewController : CNLiveBaseViewController, UITextFieldDelegate {
    
    lazy var backView : UIImageView = {
        let backView = UIImageView.init()
        backView.contentMode = .scaleToFill
        backView.clipsToBounds = true
        return backView
    }()
    
    lazy var topView: UIView = {
        let topView = UIView.init()
        topView.backgroundColor = .white
        topView.layer.cornerRadius = 8
        topView.layer.masksToBounds = true
        return topView
    }()
    
    lazy var tipLabel: UILabel = {
        let tipLabel = UILabel.init()
        tipLabel.text = "输入积分数"
        tipLabel.textColor = .black
        tipLabel.font = BoldFont_15_Fit
        return tipLabel
    }()
    
    lazy var descL: UILabel = {
        let descL = UILabel.init()
        descL.text = "可兑积分:10.28w"
        descL.textColor = HexColor("#999999")
        descL.backgroundColor = .white
        descL.font = Font_14_Fit
        return descL
    }()
    
    lazy var numberField: UITextField = {
        let numberField = UITextField.init()
        numberField.placeholder = "最低兑换100积分"
        numberField.font = BoldFont_18_Fit
        numberField.keyboardType = .decimalPad
        numberField.clearButtonMode = .whileEditing
        numberField.backgroundColor = .white
        numberField.textColor = .black
        numberField.delegate = self
        numberField.addTarget(self, action: #selector(numberChangeAction), for: .editingChanged)
        return numberField
    }()
    
    lazy var lineLabel: UILabel = {
        let lineLabel = UILabel.init()
        lineLabel.backgroundColor = HexColor("#D9DBE4")
        return lineLabel
    }()
    
    lazy var tagView: UIView = {
        let tagView = UIView.init()
        tagView.backgroundColor = .white
        return tagView
    }()
    
    lazy var bottemView: UIView = {
        let bottemView = UIView.init()
        bottemView.backgroundColor = .white
        bottemView.layer.cornerRadius = 8
        bottemView.layer.masksToBounds = true
        return bottemView
    }()
    
    lazy var bottemTipLabel: UILabel = {
        let bottemTipLabel = UILabel.init()
        bottemTipLabel.backgroundColor = .white
        bottemTipLabel.textColor = .black
        bottemTipLabel.font = BoldFont_14_Fit
        bottemTipLabel.text = "元码余额 (元)"
        return bottemTipLabel
    }()
    
    lazy var priceLabel: UILabel = {
        let priceLabel = UILabel.init()
        priceLabel.text = "0.00"
        priceLabel.textColor = HexColor("#F5A623")
        priceLabel.font = BoldFont_Fit(24)
        priceLabel.backgroundColor = .white
        return priceLabel
    }()
    
    lazy var tipBtn: UIButton = {
        let tipBtn = UIButton.init(type: .custom)
        tipBtn.setTitle("至少兑换100积分", for: .normal)
        tipBtn.setTitleColor(HexColor("#E02020"), for: .normal)
        tipBtn.setImage(UIImage(named: "mine_header_quest_tip_red"), for: .normal)
        tipBtn.titleLabel?.font = Font_11
        tipBtn.contentHorizontalAlignment = .left
        tipBtn.isHidden = true
        tipBtn.setupButtonImageAndTitlePossitionWith(padding: 3, style: .left)
        return tipBtn
    }()
    
    lazy var descBtn: UIButton = {
        let descBtn = UIButton.init(type: .custom)
        descBtn.setTitle("最低100积分,不足100的积分将原路返回", for: .normal)
        descBtn.setTitleColor(HexColor("#999999"), for: .normal)
        descBtn.setImage(UIImage(named: "mine_header_quest_tip_black"), for: .normal)
        descBtn.titleLabel?.font = Font_11
        descBtn.contentHorizontalAlignment = .left
        descBtn.setupButtonImageAndTitlePossitionWith(padding: 3, style: .left)
        return descBtn
    }()
    
    lazy var sendButton: UIButton = {
        let sendButton = UIButton(type: .custom)
        sendButton.setTitle("立即兑换", for: .normal)
        sendButton.setTitleColor(.white, for: .normal)
        sendButton.titleLabel?.font = BoldFont_16_Fit
        sendButton.layer.cornerRadius = 20
        sendButton.layer.masksToBounds = true
        sendButton.addTarget(self, action: #selector(sendInfoActionMothod), for: .touchUpInside)
        return sendButton
    }()
    
    var tempBtn: UIButton!
    var tempString: String!
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarHidden = false
        titleName = "积分兑换"
        titleLabel.textColor = .white
        leftBtnWhite = true
        topNavView.backgroundColor = .clear
        setNeedsStatusBarAppearanceUpdate()
        
        view.insertSubview(backView, belowSubview: topNavView)
        view.addSubview(topView)
        topView.addSubview(tipLabel)
        topView.addSubview(descL)
        topView.addSubview(numberField)
        topView.addSubview(lineLabel)
        topView.addSubview(tagView)
        topView.addSubview(tipBtn)
        view.addSubview(bottemView)
        bottemView.addSubview(bottemTipLabel)
        bottemView.addSubview(priceLabel)
        view.addSubview(descBtn)
        view.addSubview(sendButton)
        setupWithContent()
        setupConstraints()
        
        setupWithSendBtnStatue(statue: false)
    }
    
    func setupConstraints() {
        backView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(210)
        }
        topView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(get_system_nav_height()+30)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(150)
        }
        tipLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(15)
            make.left.equalToSuperview().offset(20)
        }
        descL.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-20)
        }
        numberField.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(40)
        }
        lineLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.height.equalTo(0.5)
            make.top.equalTo(numberField.snp.bottom)
        }
        tagView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.height.equalTo(30)
            make.top.equalTo(lineLabel.snp.bottom).offset(10)
        }
        tipBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.top.equalTo(tagView.snp.bottom).offset(10)
            make.height.equalTo(15)
            make.width.equalTo(180)
        }
        
        bottemView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(topView.snp.bottom).offset(20)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(90)
        }
        bottemTipLabel.snp.makeConstraints { make in
            make.left.top.equalToSuperview().offset(20)
        }
        priceLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.bottom.equalToSuperview().offset(-10)
        }
        
        descBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(30)
            make.right.equalToSuperview().offset(-20)
            make.top.equalTo(bottemView.snp.bottom).offset(10)
            make.height.equalTo(15)
            make.width.equalTo(180)
        }
        sendButton.snp.makeConstraints { make in
            make.top.equalTo(descBtn.snp.bottom).offset(50)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(40)
        }
        backView.setGradient(colors: [UIColor(red: 0/255.0, green: 106/255.0, blue: 229/255.0, alpha: 1),UIColor(red: 247/255.0, green: 248/255.0, blue: 250/255.0, alpha: 1)], startPoint: CGPoint(x: 0.5, y: 0), endPoint: CGPoint(x: 0.5, y: 1))
    }
    
    func setupWithContent() {
        let array: NSArray = ["1000","2000","3000","全部兑换"]
        for (index,item) in array.enumerated() {
            let w = (KSreenWidth-90)/4
            let x = (index * Int(w))+(index * 10)
            let numberBtn = UIButton.init(type: .custom)
            numberBtn.frame = CGRect(x: Int(x), y: 0, width: Int(w), height: 30)
            numberBtn.backgroundColor = HexColor("#F7F8FA")
            numberBtn.setTitle((item as! String), for: .normal)
            numberBtn.layer.cornerRadius = 5
            numberBtn.layer.masksToBounds = true
            numberBtn.tag = 1000 + index
            numberBtn.titleLabel?.font = Font_14_Fit
            numberBtn.setTitleColor(HexColor("#999999"), for: .normal)
            numberBtn.addTarget(self, action: #selector(numberAction), for: .touchUpInside)
            tagView.addSubview(numberBtn)
        }
    }
    
    private func setupWithSendBtnStatue(statue: Bool) {
        var alpha = 0.3
        if statue {
            alpha = 1
        }
        sendButton.setGradient(colors: [UIColor(red: 103/255.0, green: 197/255.0, blue: 255/255.0, alpha: alpha),UIColor(red: 96/255.0, green: 154/255.0, blue: 255/255.0, alpha: alpha)], startPoint: CGPoint(x: 0, y: 0.5), endPoint: CGPoint(x: 1, y: 0.5))
        sendButton.isUserInteractionEnabled = statue
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    @objc func sendInfoActionMothod() {
        
    }
    
    @objc func numberAction(sender: UIButton) {
        showErrorMessage(message: "", clear: true)
        sender.backgroundColor = HexColor("#FFF8EE")
        sender.layer.borderColor = HexColor("#F5A623")?.cgColor
        sender.layer.borderWidth = 1
        sender.setTitleColor(HexColor("#F5A623"), for: .normal)
        if tempBtn != nil {
            tempBtn.backgroundColor = HexColor("#F7F8FA")
            tempBtn.layer.borderColor = HexColor("#F7F8FA")?.cgColor
            tempBtn.layer.borderWidth = 0
            tempBtn.setTitleColor(HexColor("#999999"), for: .normal)
        }
        if sender.tag == 1000 {
            numberField.text = "1000"
        }
        if sender.tag == 1001 {
            numberField.text = "2000"
        }
        if sender.tag == 1002 {
            numberField.text = "3000"
        }
        if sender.tag == 1003 {
            numberField.text = "全部"
        }
        tempBtn = sender
    }
}

extension CNLiveMineExchangeViewController {
    
    @objc func numberChangeAction(textField: UITextField) {
        if textField.text?.count == 0 {
            tempString = ""
        }
        if isPurerFloat(str: textField.text!) {
            textField.text = tempString
        }
        if !isPurerFloat(str: textField.text!) && textField.text!.count <= 10 {
            tempString = textField.text
        }
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField == numberField {
            if !checkDecimal(str: textField.text?.appending(string) ?? "") {
                if textField.text!.count > 0 && string == "." && !textField.text!.contains(".") {
                    return true
                }
                return string == ""
            }
        }
        return true
    }
    
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        if textField == numberField && !textField.text!.isEmpty && textField.text!.count > 10 {
            if !textField.text!.contains(".") {
                textField.text = textField.text!.prefix(10).lowercased()
            }else{
                let range = textField.text!.range(of: ".")
                let new_text = textField.text!.suffix(from: range!.upperBound)
                if new_text.count == 0 {
                    textField.text = textField.text!+"00"
                }
                if new_text.count == 1 {
                    textField.text = textField.text!+"0"
                }
            }
        }
        return true
    }
    
    /// 已经结束输入的时候调用
    /// - Parameter textField: textField
    func textFieldDidEndEditing(_ textField: UITextField) {
        if textField == numberField {
            let number = (textField.text! as NSString).floatValue
            if number < 100 {
                showErrorMessage(message: "至少兑换100积分")
            }
            if number > 10000 {
                showErrorMessage(message: "超过已经拥有积分数")
            }
        }
    }
    
    /// 开始显示的时候去掉提示
    /// - Parameter textField: textField
    /// - Returns:
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        showErrorMessage(message: "", clear: true)
        return true
    }
    
    /// 显示下面的提示框
    /// - Parameters:
    ///   - message: 提示消息
    ///   - clear: 是否情况消息
    private func showErrorMessage(message: String,clear: Bool = false) {
        if clear {
            tipBtn.isHidden = true
            topView.snp.remakeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalTo(get_system_nav_height()+30)
                make.left.equalToSuperview().offset(20)
                make.right.equalToSuperview().offset(-20)
                make.height.equalTo(150)
            }
            return
        }
        tipBtn.setTitle(message, for: .normal)
        tipBtn.isHidden = false
        topView.snp.remakeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(get_system_nav_height()+30)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(170)
        }
    }
    
    /// 判断是不是小数
    /// - Parameter str: 需要判断的字符串
    /// - Returns: 返回
    private func isPurerFloat(str: String) -> Bool {
        let scan = Scanner(string: str)
        var val: Float = 0.0
        if scan.scanHexFloat(&val) && scan.isAtEnd {
            if Float(str)! >= 0 {
                return true
            } else {
                return false
            }
        }
        return false
    }
    
    /// 判断是不是两位小数
    /// - Parameter str: str
    private func checkDecimal(str: String) -> Bool {
        let pred = NSPredicate(format: "SELF MATCHES %@", "^[0-9]+(\\.[0-9]{1,2})?$")
        return pred.evaluate(with: str)
    }
    
}