iOS(Swift)—仿支付宝我的二维码页面系统亮度调整

来源:互联网 发布:微信秒红包软件 编辑:程序博客网 时间:2024/05/17 22:18

之前已经把OC的实现方案完成,现在我就不介绍逻辑了,想必懂swift的朋友一定懂得OC吧

这里只把资源的链接附上

OC_Demo下载地址

Swift_Demo下载地址

还是把主要的代码贴一下

1. AppDelegate.swift中代码:

// 程序启动完成    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {        // Override point for customization after application launch.        self.window = UIWindow (frame: UIScreen.main.bounds)        self.window?.rootViewController = UINavigationController (rootViewController: ViewController())        self.window?.makeKeyAndVisible()        USER_DEFAULTS .set(false, forKey: ISQRCONTROLLER)        return true    }// 正在使用应用的时候按“home”键,恢复系统的亮度    func applicationWillResignActive(_ application: UIApplication) {        let brights:float_t = USER_DEFAULTS .value(forKey: SCREEN_BRIGHT) as! Float        print("当前亮度00: \(brights)")        UIScreen.main.brightness = CGFloat(brights)    }//  应用成前台运行的时候,(即上次再使用APP的时候,突然按“home”键后,过一定时间又想用APP,如果是从二维码页面退出到后台,需要使二维码页面变亮(这时二维码的控制器中的几个方法不起作用,就靠这里了))    func applicationDidBecomeActive(_ application: UIApplication) {        let isQR = Bool(USER_DEFAULTS .value(forKey: ISQRCONTROLLER) as! Bool)        if isQR {            UIScreen.main.brightness = 0.6        }else{            USER_DEFAULTS .set(UIScreen.main.brightness, forKey: SCREEN_BRIGHT)        }    }


2. ViewController.swift就是一个过渡页面,里面一个按钮,点击该按钮跳转到二维码二面:

// ViewController.swiftimport UIKitclass ViewController: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()        self.view.backgroundColor = UIColor .white        let qRBtn:UIButton = UIButton (frame: CGRect(x: 60, y:100, width: 90, height: 30))        qRBtn.layer.borderColor = UIColor .blue.cgColor        qRBtn.layer.borderWidth = 1.0        qRBtn.layer.cornerRadius = 5.0        qRBtn.layer.masksToBounds = true        qRBtn .setTitle("我的二维码", for: UIControlState.normal)        qRBtn.titleLabel?.font = UIFont .systemFont(ofSize: 14)        qRBtn .setTitleColor(UIColor.blue, for: UIControlState.normal)        qRBtn .addTarget(self, action: #selector(ViewController.btnClick), for: UIControlEvents.touchUpInside)        self.view .addSubview(qRBtn)    }    @objc func btnClick() -> Void {        self.navigationController?.pushViewController(QRCodeController(), animated: true)    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }}


3. QRCodeController.swift中代码:

//  Created by Lijinkui on 2017/10/25.//  Copyright © 2017 year Lijinkui. All rights reserved.//import UIKitpublic let ScreenW = UIScreen.main.bounds.widthpublic let ScreenH = UIScreen.main.bounds.heightpublic let USER_DEFAULTS = UserDefaults.standardpublic let SCREEN_BRIGHT = "screenBrights"public let ISQRCONTROLLER = "isQRController"var currentLight: CGFloat = 0;class QRCodeController: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()        self .title = "我的二维码"        self.view?.backgroundColor = UIColor .white        let qrImg: UIImageView = UIImageView (frame: CGRect(x: ScreenW * 0.1, y: ScreenH/2 - (ScreenW*0.4), width: ScreenW * 0.8, height: ScreenW * 0.8))        qrImg.image = UIImage (named: "CodeImg")        self.view?.addSubview(qrImg)    }    // 把将要进入二维码页面时的系统亮度保存    override func viewWillAppear(_ animated: Bool) {        currentLight = CGFloat(USER_DEFAULTS .value(forKey: SCREEN_BRIGHT) as! Float)        USER_DEFAULTS .set(true, forKey: ISQRCONTROLLER)    }    // 进入控制器完成后,让控制器高亮    override func viewDidAppear(_ animated: Bool) {        UIScreen.main .brightness = 0.6    }    // 退出控制器时恢复之前的亮度      override func viewWillDisappear(_ animated: Bool) {        UIScreen.main.brightness = currentLight        USER_DEFAULTS .set(false, forKey: ISQRCONTROLLER)    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }}


原创粉丝点击