本文共 3507 字,大约阅读时间需要 11 分钟。
//// CustomView.swift// DemoApp//// Created by liuan on 2020/5/3.// Copyright © 2020 anguo.com. All rights reserved.//import UIKit//自定义文本视图class CustomView: UIView,UITextFieldDelegate { var textField:UITextField! weak var controllor : ViewController? override init(frame:CGRect){ super.init(frame: frame) textField=UITextField(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)) textField.font=UIFont.boldSystemFont(ofSize: 14) textField.textColor = .purple textField.layer.shadowColor=UIColor.black.cgColor textField.layer.shadowOffset = CGSize(width: 0.0, height: 3.0) textField.layer.shadowOpacity=0.45 textField.layer.shadowRadius=3 textField.backgroundColor = .lightGray textField.delegate=self self.addSubview(textField) } func textFieldShouldReturn(_ textField: UITextField) -> Bool { NotificationCenter.default.post(name: Notification.Name(rawValue: "checkFromNotification"),object: nil,userInfo: nil) return true } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
//// ViewController.swift// DemoApp//// Created by liuan on 2020/4/23.// Copyright © 2020 anguo.com. All rights reserved.//import UIKitclass ViewController: UIViewController,UITextFieldDelegate { var nameFiled:CustomView! var passwordField:CustomView! var submitButton:UIButton! var animationView:UIView! override func viewDidLoad(){ super.viewDidLoad() let width=Int(self.view.frame.size.width)-40 let height=40 self.nameFiled = CustomView(frame: CGRect(x: 20, y: 60, width: width, height: height)) self.view.addSubview(self.nameFiled) self.passwordField = CustomView(frame: CGRect(x: 20, y: 140, width: width, height: height)) self.view.addSubview(self.passwordField) self.submitButton = UIButton(frame: CGRect(x: 20, y: 240, width: width, height: height)) self.submitButton.setTitle("提交", for: .normal) self.submitButton.backgroundColor = .gray self.submitButton.isEnabled=false self.submitButton.addTarget(self, action: #selector(ViewController.submitForm(_:)), for: .touchUpInside) self.view.addSubview(self.submitButton) NotificationCenter.default.addObserver(self, selector: #selector(checkForm(_:)), name: NSNotification.Name(rawValue: "checkFromNotification"), object: nil) } @objc func submitForm(_ sender:UIButton){ print("submitForm(_:)") } @objc func checkForm(_ notifiy:Notification?){ if self.nameFiled.textField.text != "" && passwordField.textField.text != ""{ self.submitButton.isEnabled = true self.submitButton.backgroundColor = .orange }else{ self.submitButton.isEnabled = false self.submitButton.backgroundColor = .gray } } func textFieldShouldReturn(_ textField: UITextField) -> Bool { checkForm() return true } func checkForm() { if self.nameFiled.textField.text != "" && self.passwordField.textField.text != "" { self.submitButton.isEnabled=true self.submitButton.backgroundColor = .orange }else{ self.submitButton.isEnabled=false self.submitButton.backgroundColor = .gray } } deinit { NotificationCenter.default.removeObserver(self) } }
转载地址:http://ltsr.baihongyu.com/