UIView的程序化"模糊"风格背景

Fat*_*tie 10 image-processing ios dynamic-image-generation gpuimage swift

当然,为背景设置纯色是微不足道的:

在此输入图像描述

现在,使用"模糊"或"阴天"背景作为应用程序中的设计功能,而不是使用"普通灰色".

例如,这里有一对"模糊"的背景 - 它只是一种简单的颜色,可能有一些噪音,也可能是模糊的.

你可以看到这样的背景,考虑流行的饲料应用程序(whassapp等).这是我们今天的"时尚".

在此输入图像描述

在此输入图像描述

它发生在我身上,如果你能在Swift的代码中做到这一点,那就太棒了

注意:从PNG开始并不是一个优雅的解决方案:

希望可以从头开始以编程方式生成所有内容.

如果Inspector有一个IBDesignable风格的滑块,"添加时尚'颗粒'背景......",那将是很棒的- 在新时代应该是可能的!

Som*_*Guy 14

根据我很久以前写过的内容,这将帮助您入门:

在此输入图像描述

@IBInspectable 特性:

  • noiseColor:噪声/颗粒颜色,这是应用于视图的 backgroundColor
  • noiseMinAlpha:随机噪声的最小alpha值
  • noiseMaxAlpha:随机噪声的最大alpha值
  • noisePasses:施加噪声多少次,更多的通过会更慢,但可以产生更好的噪音效果
  • noiseSpacing:随机噪声的发生有多常见,间距越大意味着噪声越少

说明:

当任何可设计的噪声属性发生更改时,视图将被标记为重绘.在绘制函数UIImage中生成(或从NSCache可用时拉出).

在生成方法中,迭代每个像素,并且如果像素应该是噪声(取决于间隔参数),则使用随机化的α通道应用噪声颜色.这与通过次数一样多次.

.

// NoiseView.swift
import UIKit

let noiseImageCache = NSCache()

@IBDesignable class NoiseView: UIView {

    let noiseImageSize = CGSizeMake(128, 128)

    @IBInspectable var noiseColor: UIColor = UIColor.blackColor() {
        didSet { setNeedsDisplay() }
    }
    @IBInspectable var noiseMinAlpha: CGFloat = 0 {
        didSet { setNeedsDisplay() }
    }
    @IBInspectable var noiseMaxAlpha: CGFloat = 1 {
        didSet { setNeedsDisplay() }
    }
    @IBInspectable var noisePasses: Int = 1 {
        didSet {
            noisePasses = max(0, noisePasses)
            setNeedsDisplay()
        }
    }
    @IBInspectable var noiseSpacing: Int = 1 {
        didSet {
            noiseSpacing = max(1, noiseSpacing)
            setNeedsDisplay()
        }
    }

    override func drawRect(rect: CGRect) {
        super.drawRect(rect)

        UIColor(patternImage: currentUIImage()).set()
        UIRectFillUsingBlendMode(bounds, .Normal)
    }

    private func currentUIImage() -> UIImage {

        //  Key based on all parameters
        let cacheKey = "\(noiseImageSize),\(noiseColor),\(noiseMinAlpha),\(noiseMaxAlpha),\(noisePasses)"

        var image = noiseImageCache.objectForKey(cacheKey) as! UIImage!

        if image == nil {
            image = generatedUIImage()

            #if !TARGET_INTERFACE_BUILDER
                noiseImageCache.setObject(image, forKey: cacheKey)
            #endif
        }

        return image
    }

    private func generatedUIImage() -> UIImage {

        UIGraphicsBeginImageContextWithOptions(noiseImageSize, false, 0)

        let accuracy: CGFloat = 1000.0

        for _ in 0..<noisePasses {
            for y in 0..<Int(noiseImageSize.height) {
                for x in 0..<Int(noiseImageSize.width) {
                    if random() % noiseSpacing == 0 {
                        let alpha = (CGFloat(random() % Int((noiseMaxAlpha - noiseMinAlpha) * accuracy)) / accuracy) + noiseMinAlpha
                        noiseColor.colorWithAlphaComponent(alpha).set()
                        UIRectFill(CGRectMake(CGFloat(x), CGFloat(y), 1, 1))
                    }
                }
            }
        }

        let image = UIGraphicsGetImageFromCurrentImageContext() as UIImage

        UIGraphicsEndImageContext()

        return image
    }
}
Run Code Online (Sandbox Code Playgroud)