直接上代码吧
import UIKit
class LogViewController: UIViewController {
@IBOutlet weak var logIMG: UIImageView!
@IBOutlet weak var passwordText: UILabel!
@IBOutlet weak var accountText: UILabel!
@IBOutlet weak var hint: UILabel!
@IBOutlet weak var logButton: UIButton!
@IBOutlet weak var account: UITextField!
@IBOutlet weak var password: UITextField!
var boo = true
//MARK: 记录UITextView的原始高度
var textViewHeight: CGFloat!
override func viewDidLoad() {
super.viewDidLoad()
//监听键盘改变
//弹出时
NotificationCenter.default.addObserver(self, selector:#selector(ShowNotification), name:UIResponder.keyboardWillShowNotification, object: nil)
//弹回时
NotificationCenter.default.addObserver(self, selector:#selector(HideNotification), name:UIResponder.keyboardWillHideNotification, object: nil)
setStyle()
self.logButton.frame = CGRect(x: self.view.bounds.width*0.125, y: self.view.bounds.height*0.85, width: self.view.bounds.width*0.75,height: 45)
self.hint.frame = CGRect(x: self.view.bounds.width*0.45, y: self.view.bounds.height*0.85+45, width: 120,height: 30)
//设置按钮点击s事件
self.logButton.addTarget(self,action:#selector(buttonClick), for: .touchUpInside)
self.view.addSubview(logButton)
}
func setStyle(){
logIMG.frame = CGRect(x: self.view.bounds.width*0.3, y: self.view.bounds.height*0.25, width: self.view.bounds.width*0.4,height: self.view.bounds.width*0.4)
logIMG.layer.cornerRadius = 10.0
self.logButton.layer.cornerRadius = 8.0;//设置圆角的弧度
//为了适配屏幕尺寸对每个标签都进行单独尺寸和位置设置来适应吗,每个手机的尺寸
self.accountText.frame = CGRect(x: self.view.bounds.width*0.125, y: self.view.bounds.height*0.65, width: self.view.bounds.width*0.2,height: 35)
self.account.frame = CGRect(x: self.view.bounds.width*0.3, y: self.view.bounds.height*0.65, width: self.view.bounds.width*0.6,height: 30)
self.passwordText.frame = CGRect(x: self.view.bounds.width*0.125, y: self.view.bounds.height*0.75, width: self.view.bounds.width*0.2,height: 30)
self.password.frame = CGRect(x: self.view.bounds.width*0.3, y: self.view.bounds.height*0.75, width: self.view.bounds.width*0.6,height: 35)
}
//弹出时改变样式
@objc func ShowNotification(note:NSNotification){
//得到键盘高度
let info:NSDictionary = note.userInfo as! NSDictionary
let keyValue = info.object(forKey: "UIKeyboardFrameEndUserInfoKey")
let keyRect = (keyValue as AnyObject).cgRectValue
let height = keyRect?.size.height
print(height!)
logIMG.frame = CGRect(x: self.view.bounds.width*0.3, y: self.view.bounds.height*0.25-height!*0.8, width: self.view.bounds.width*0.4,height: self.view.bounds.width*0.4)
self.accountText.frame = CGRect(x: self.view.bounds.width*0.125, y: self.view.bounds.height*0.65-height!*0.8, width: self.view.bounds.width*0.2,height: 35)
self.account.frame = CGRect(x: self.view.bounds.width*0.3, y: self.view.bounds.height*0.65-height!*0.8, width: self.view.bounds.width*0.6,height: 30)
self.passwordText.frame = CGRect(x: self.view.bounds.width*0.125, y: self.view.bounds.height*0.75-height!*0.8, width: self.view.bounds.width*0.2,height: 30)
self.password.frame = CGRect(x: self.view.bounds.width*0.3, y: self.view.bounds.height*0.75-height!*0.8, width: self.view.bounds.width*0.6,height: 35)
}
//恢复时样式重新变回原来的
@objc func HideNotification(note:NSNotification){
setStyle()
}
//好像是点击键盘外键盘tian回
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}
// 移除监听
deinit{
NotificationCenter.default.removeObserver(self)
}
@objc func buttonClick(){
//用于p判断是否和后台交互成功
var boo = false
// 获取输入框输入的文本
let str:String? = account.text
let pwd:String? = password.text
let url = URL(string:"https://dhwl.xyz:8441/IOSLogin?uname=\(account.text!)&password=\(password.text!)");
var request = URLRequest(url: url!)
let list = NSMutableArray()
var paramDic = [String: String]()
if paramDic.count > 0 {
//设置为POST请求
request.httpMethod = "GET"
//拆分字典,subDic是其中一项,将key与value变成字符串
for subDic in paramDic {
let tmpStr = "\(subDic.0)=\(subDic.1)"
list.add(tmpStr)
}
//用&拼接变成字符串的字典各项
let paramStr = list.componentsJoined(by: "&")
//UTF8转码,防止汉字符号引起的非法网址
let paraData = paramStr.data(using: String.Encoding.utf8)
//设置请求体
request.httpBody = paraData
}
let configuration:URLSessionConfiguration = URLSessionConfiguration.default
let session:URLSession = URLSession(configuration: configuration)
//j一个判断是否请求结束的变量
var semaphore = DispatchSemaphore(value:0)
let task:URLSessionDataTask = session.dataTask(with: request) { (data, response, error)->Void in
if error == nil{
if data == nil{
return
}
//b捕获异常如果强转失败会返回nil 不是nil 证明成功来进行一系列的操作
do{
let responseData = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as? NSDictionary
if(responseData != nil){
boo = true
//字典获取值并进行赋值
let data: NSDictionary = responseData!.value(forKey: "datasStatistics")as! NSDictionary
let longitudeLatitude = responseData!.value(forKey: "longitudeLatitude")as! NSArray
Datas.setDatas(datasStatistics: data)
LongitudeLatitudeList.setLongitudeLatitudeList(longitudeLatitude: longitudeLatitude)
boo = true
UseRoot.setDatas(account: str!, password: pwd!)
//为请求变量进行加载
semaphore.signal()
}else{
//为请求变量进行加载
semaphore.signal()
return
}
}catch{
print("catch")
}
}else{
print("error:\(error)")
return
}
}
// 启动任务
task.resume()
//个人理解类似于阻塞没j请求结束不回向下执行
var sess = semaphore.wait(timeout: DispatchTime.distantFuture)
//判断如果请求成功就跳转到对应的页面
if(boo){
//跳转到定义好的storyboard
let vc = UIStoryboard(name: "Main", bundle: nil)
//storyboard中定义的 storyboard ID 不是传的类名
let home = vc.instantiateViewController(withIdentifier:"HomeTabBarController")
self.navigationController?.isNavigationBarHidden = false
self.navigationController?.pushViewController(home, animated: true)
hint.text = ""
}else{
hint.text = "密码错误"
}
}
@IBAction func backClicked(butt:UIButton){
self.dismiss(animated: true, completion: nil)
navigationController?.setNavigationBarHidden(true, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
}
键盘的弹入和弹出实现页面上划其实就是过去键盘的划入划出事件然后在动态获取页面定义UI组件的高度每次键盘弹出就在弹出方法中把页面主键的y轴原先定义的位置减去键盘的高度在键盘复位时在在对应的方法中把原先定义的主键位置在重新赋一边值,具体代码片段
//监听键盘改变
//弹出时
NotificationCenter.default.addObserver(self, selector:#selector(ShowNotification), name:UIResponder.keyboardWillShowNotification, object: nil)
//弹回时
NotificationCenter.default.addObserver(self, selector:#selector(HideNotification), name:UIResponder.keyboardWillHideNotification, object: nil)
//设置页面主键位置函数
setStyle()
//弹出时改变样式得到键盘高度在改变一下页面定义UI组件的视图位置
@objc func ShowNotification(note:NSNotification){
//得到键盘高度
let info:NSDictionary = note.userInfo as! NSDictionary
let keyValue = info.object(forKey: "UIKeyboardFrameEndUserInfoKey")
let keyRect = (keyValue as AnyObject).cgRectValue
let height = keyRect?.size.height
print(height!)
logIMG.frame = CGRect(x: self.view.bounds.width*0.3, y: self.view.bounds.height*0.25-height!*0.8, width: self.view.bounds.width*0.4,height: self.view.bounds.width*0.4)
self.accountText.frame = CGRect(x: self.view.bounds.width*0.125, y: self.view.bounds.height*0.65-height!*0.8, width: self.view.bounds.width*0.2,height: 35)
self.account.frame = CGRect(x: self.view.bounds.width*0.3, y: self.view.bounds.height*0.65-height!*0.8, width: self.view.bounds.width*0.6,height: 30)
self.passwordText.frame = CGRect(x: self.view.bounds.width*0.125, y: self.view.bounds.height*0.75-height!*0.8, width: self.view.bounds.width*0.2,height: 30)
self.password.frame = CGRect(x: self.view.bounds.width*0.3, y: self.view.bounds.height*0.75-height!*0.8, width: self.view.bounds.width*0.6,height: 35)
}
//恢复时样式重新变回原来的
@objc func HideNotification(note:NSNotification){
setStyle()
}
OK