第六天:SwiftPopUPAndFadeOut
Posted CHM
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第六天:SwiftPopUPAndFadeOut相关的知识,希望对你有一定的参考价值。
参考链接:https://www.jianshu.com/p/632fdaede292
1 import UIKit 2 3 // 先写两个屏幕宽高的宏定义 4 let kScreenWidth = UIScreen.main.bounds.size.width 5 let kScreenHeight = UIScreen.main.bounds.size.height 6 7 class ViewController: UIViewController { 8 9 // 定义三个属性 10 // 背景View 11 // 弹出View 12 // 弹出View 的 Label 13 var bkgView: UIView! 14 var popView: UIView! 15 var popLabel: UILabel! 16 17 override func viewDidLoad() { 18 super.viewDidLoad() 19 // Do any additional setup after loading the view, typically from a nib. 20 setupUI() 21 } 22 23 24 // 配置 UI 方法 func setupUI() { } 25 func setupUI() { 26 // self.view 的颜色 27 self.view.backgroundColor = UIColor.cyan 28 29 // 创建 弹出 Button 30 let popBtn = UIButton() 31 popBtn.frame.size = CGSize(width: 100.0, height: 100.0) 32 popBtn.center = self.view.center 33 popBtn.layer.cornerRadius = 50.0 34 popBtn.backgroundColor = UIColor.green 35 popBtn.setTitle("PopUP", for: .normal) 36 37 popBtn.addTarget(self, action: #selector(popUpAction), for: .touchUpInside) 38 39 self.view.addSubview(popBtn) 40 } 41 42 override func didReceiveMemoryWarning() { 43 super.didReceiveMemoryWarning() 44 // Dispose of any resources that can be recreated. 45 } 46 } 47 48 // 延展 49 extension ViewController { 50 @objc func popUpAction() { 51 52 guard let windows = UIApplication.shared.keyWindow else { 53 return 54 } 55 56 bkgView = UIView() 57 bkgView.frame = windows.bounds 58 bkgView.backgroundColor = UIColor (white: 0.3, alpha: 0.6) 59 windows.addSubview(bkgView) 60 61 popView = UIView() 62 popView.frame = CGRect(x: 30.0, y: kScreenHeight, width: kScreenWidth - 60.0, height: 30.0) 63 popView.backgroundColor = UIColor.orange 64 popView.layer.cornerRadius = 15.0 65 windows.addSubview(popView) 66 67 UIView.animate(withDuration: 0.3) { 68 self.popView.frame = CGRect(x: 30.0, y: (kScreenHeight - 30) / 2.0, width: kScreenWidth - 60.0, height: 30.0) 69 } 70 71 let tapGesture = UITapGestureRecognizer(target: self, action: #selector(hideView)) 72 bkgView.addGestureRecognizer(tapGesture) 73 } 74 75 @objc func hideView() { 76 UIView.animate(withDuration: 0.3) { 77 self.popView.frame = CGRect(x: 30.0, y: kScreenHeight, width: kScreenWidth - 60.0, height: 30.0) 78 79 DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: { 80 self.popView.removeFromSuperview() 81 self.bkgView.removeFromSuperview() 82 }) 83 } 84 } 85 86 }
以上是关于第六天:SwiftPopUPAndFadeOut的主要内容,如果未能解决你的问题,请参考以下文章