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

之前已经把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.swift
import UIKit

class 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 UIKit

public let ScreenW = UIScreen.main.bounds.width
public let ScreenH = UIScreen.main.bounds.height
public let USER_DEFAULTS = UserDefaults.standard
public 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.
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

启程Boy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值