1. 控制器中viewDidLoad使用 要在主线程
///更新动画 DispatchQueue.main.async { UIView.animate(withDuration: 0.5) {[weak self] in self?.centerIV.snp.updateConstraints { (make) in make.top.equalTo(TGSNavBarHeight + 5) make.width.equalTo(TGSScaleWidth(value: 202)) make.height.equalTo(TGSScaleWidth(value: 25)) } self?.loginView.snp.updateConstraints({ (make) in make.top.equalTo(loginViewY) }) ///核心代码 self?.view.layoutIfNeeded() } }
2. 其他地方 不需要主线程也能实现自定义点击事件, 自定义view
/// 点击事件 private func clickEvent(){ loginView.clickHandle = {[weak self] (clickType) in switch clickType { case .close: if let strongSelf = self{ UIView.animate(withDuration: 0.5) { strongSelf.centerIV.snp.updateConstraints { (make) in make.top.equalToSuperview().offset(TGSScreenHeight*0.5) make.width.equalTo(TGSScaleWidth(value: 270)) make.height.equalTo(TGSScaleWidth(value: 33)) } strongSelf.loginView.snp.updateConstraints { (make) in make.top.equalTo(TGSScreenHeight) } strongSelf.view.layoutIfNeeded() } completion: { (_) in strongSelf.navigationController?.popViewController(animated: true) } } case .clickCodeBtn(let phoneNum): TGSLOG(message: "phoneNum = \(phoneNum)") ///开始请求 -》 请求成功 启动定时器/弹框 self?.loginView.startCountdown() default:break } } }