Skip to content

A subclass of UIView used to achieve application gray screen effect.(一个UIView的子类,用以实现应用灰屏效果。)

License

Notifications You must be signed in to change notification settings

Sfh03031/UIOverlayView

Repository files navigation

Example

UIOverlayView

CI Status Version License Platform

Introduction

UIOverlayView is a subclass of UIView used to achieve application gray screen effect.(UIOverlayView是一个UIView的子类,用以实现应用灰屏效果。)

To run the example project, clone the repo, and run pod install from the Example directory first.

DeepWiki

Requirements

  • iOS 12.0 or later
  • Swift 5.9.2
  • Xcode 15.1

Installation

UIOverlayView is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'UIOverlayView'

If you want to use the latest features of UIOverlayView use normal external source dependencies.

pod 'UIOverlayView', :git => 'https://github.com/Sfh03031/UIOverlayView.git'

Usage

First, install and import UIOverlayView

import UIOverlayView

Second, use class methods of UIOverlayView where you needed to show or hide

# show overlay
UIOverlayView.show()

# hide overlay
UIOverlayView.hide()

It's that simple, enjoy it.

UIOverlayView also provides 16 rendering modes in the form of enumeration, Like this:

public enum UIOverlayViewBlendMode: String, CaseIterable {
    case normalBlendMode     = "normalBlendMode"
    case darkenBlendMode     = "darkenBlendMode"
    case multiplyBlendMode   = "multiplyBlendMode"
    case colorBurnBlendMode  = "colorBurnBlendMode"
    case lightenBlendMode    = "lightenBlendMode"
    case screenBlendMode     = "screenBlendMode"
    case colorDodgeBlendMode = "colorDodgeBlendMode"
    case overlayBlendMode    = "overlayBlendMode"
    case softLightBlendMode  = "softLightBlendMode"
    case hardLightBlendMode  = "hardLightBlendMode"
    case differenceBlendMode = "differenceBlendMode"
    case exclusionBlendMode  = "exclusionBlendMode"
    case hueBlendMode        = "hueBlendMode"
    case saturationBlendMode = "saturationBlendMode" // default
    case colorBlendMode      = "colorBlendMode"
    case luminosityBlendMode = "luminosityBlendMode"
}

and default mode is saturationBlendMode, default background color is lightGray, You can try to customize the effect you want with different combinations,UIOverlayView alse offers instance setting methods for rendering modes and background colors for easy customization, like this:

public extension UIOverlayView {
    
    /// set compositingFilter of layer
    /// - Parameter blendMode: case of UIOverlayViewBlendMode
    /// - Returns: Self
    func blend(mode blendMode: UIOverlayViewBlendMode) -> Self {
        self.layer.compositingFilter = blendMode.rawValue
        return self
    }
    
    /// set background color
    /// - Parameter kcolor: color
    /// - Returns: Self
    func background(color kcolor: UIColor) -> Self {
        self.backgroundColor = kcolor
        return self
    }
    
    /// add overlay
    /// - Parameter container: superView
    func overlay(in container: UIView) {
        self.frame = container.bounds
        container.addSubview(self)
    }
    
    /// remove overlay
    func remove() {
        self.removeFromSuperview()
    }
}

The superView of UIOverlayView defaults to keyWindow, but you can also customize it.

public extension UIOverlayView {
    
    /// show overlay
    /// - Parameters:
    ///   - container: superView of UIOverlayView
    ///   - mode: blendMode of UIOverlayView
    ///   - backColor: backgroundColor of UIOverlayView
    static func show(in container: UIView? = UIApplication.shared.windows.first(where: { $0.isKeyWindow }), mode: UIOverlayViewBlendMode = .saturationBlendMode, backColor: UIColor = .lightGray) {
        if let contain = container {
            if let exist = contain.subviews.first(where: { $0 is UIOverlayView }) {
                exist.removeFromSuperview()
            }
            UIOverlayView().blend(mode: mode).background(color: backColor).overlay(in: contain)
        }
    }
    
    /// hide overlay
    /// - Parameter container: superView of UIOverlayView
    static func hide(in container: UIView? = UIApplication.shared.windows.first(where: { $0.isKeyWindow })) {
        if let contain = container {
            if let exist = contain.subviews.first(where: { $0 is UIOverlayView }) {
                exist.removeFromSuperview()
            }
        }
    }
}

if you find any issue, please start a PR.

Change log

2025.04.23, 0.1.3

  • Add new demo.gif and methods(zh: 添加新的类方法和示例gif)

2025.04.8, 0.1.2

  • Add enum of BlendModes and update readme(zh: 增加渲染模式枚举,更新readme)

2025.04.1, 0.1.1

  • Initial version(zh: 初始版本)

Author

Sfh03031, sfh894645252@163.com

License

UIOverlayView is available under the MIT license. See the LICENSE file for more info.

About

A subclass of UIView used to achieve application gray screen effect.(一个UIView的子类,用以实现应用灰屏效果。)

Resources

License

Stars

Watchers

Forks

Packages

No packages published