如何使用 swift 同时模糊多个对象

how to blur multiple objects at the same time using swift

我使用了如下代码。但它只会模糊一个位置。现在我想一次模糊多个位置

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func btn_blur(_ sender: Any) {
        // 1
        let darkBlur = UIBlurEffect(style: .dark)
        // 2
        let blurView = UIVisualEffectView(effect: darkBlur)
        blurView.frame = CGRect(x: 20, y: 30, width: 50, height: 50)
        // 3
        imageView.addSubview(blurView)

    }

}

您可以在视图控制器中存储一个框架数组,并用您的服务器响应填充它。然后使用这些帧创建模糊视图。

import UIKit

class ViewController: UIViewController {

    var frames = [CGRect]() // load this from your server data

    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func btn_blur(_ sender: Any) {
        let darkBlur = UIBlurEffect(style: .dark)

        for frame in frames {
           let blurView = UIVisualEffectView(effect: darkBlur)
           blurView.frame = frame

           imageView.addSubview(blurView)
        }
    }
}