美文网首页iOS 问答集合收藏swift
Swift4.2如何实现类似支付宝双击home键App页面毛玻璃

Swift4.2如何实现类似支付宝双击home键App页面毛玻璃

作者: 船长_ | 来源:发表于2018-11-22 20:33 被阅读42次
支付宝截屏 snap.jpg demo效果图 demo.jpg

实现原理:创建一个类(单例),这个类监听App即将离开前台,进入后台,即将进入前台,进入前台的通知,在合适的时机添加一个半透明的毛玻璃View到window上,在进入前台的时候删除这个View

核心代码

notificationCenter.addObserver(self, selector: #selector(applicationWillResignActive), name: UIApplication.willResignActiveNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(applicationDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(applicationWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)

添加view

func addView() {

    application.ignoreSnapshotOnNextApplicationLaunch()
    if let view = view {
        window?.addSubview(view)
        window?.bringSubviewToFront(view)
        view.alpha = 1.0
    }
}

移除view

func removeView(completion: (() -> Swift.Void)? = nil) {
    guard !force else {
        return
    }
    if let view = view {
        guard animation.duration > 0 else {
            view.alpha = 0
            view.removeFromSuperview()
            completion?()
            return
        }
        UIView.animate(withDuration: animation.duration, delay: animation.delay, usingSpringWithDamping: animation.dampingRatio, initialSpringVelocity: animation.velocity, options: animation.options, animations: {
            view.alpha = 0
        }, completion: { _ in
            view.removeFromSuperview()
            completion?()
        })
    }
}

实现类似支付宝,只需要传一个毛玻璃效果的view

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    // LaunchScreenSnapshot.protect()
    
    // 实现类似支付宝,只需要传一个毛玻璃效果的view
    let blurView = DynamicBlurView(frame: UIScreen.main.bounds)
    blurView.blurRadius = 10
    LaunchScreenSnapshot.protect(with: blurView, trigger: LaunchScreenSnapshot.Trigger.didEnterBackground, animation: LaunchScreenSnapshot.Animation.init(), force: false)
    
    return true
}
可自定义显示view,如下 demo.gif

demo下载示例
参考原文

相关文章

网友评论

    本文标题:Swift4.2如何实现类似支付宝双击home键App页面毛玻璃

    本文链接:https://www.haomeiwen.com/subject/oqjrqqtx.html