添加模糊效果以查看

时间:2017-10-25 22:50:26

标签: ios swift uiblureffect

我有一个视图类,用于显示具有模糊背景视图的微调器。我在运行时将其添加到另一个视图,一切正常。当我将这个视图添加到另一个ViewControllers视图时,某种效果是不可见的。视图确实被添加,我通过将背景颜色设置为绿色来检查它是否存在。但是效果本身是不可见的。我添加了视图这种方式 例如:

 self.view.addSubview(self.loadingView)
 self.loadingView.frame = CGRect(origin: .zero, size:self.view.frame.size)

。实施如下,

final class LoaderView: UIView {

    fileprivate let spinner = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
    fileprivate let effect = UIBlurEffect(style: .light)
    fileprivate let backgroundView = UIVisualEffectView(frame:.zero)

    override init(frame: CGRect) {

        spinner.startAnimating()

        super.init(frame: frame)
        backgroundView.effect = effect

        addSubview(backgroundView)
        addSubview(spinner)
    }

    @available(*, unavailable)
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setSpinnerColor(color:UIColor){
        spinner.color = color
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        backgroundView.frame = CGRect(x: 0, y: 0, width: self.bounds.height/5, height: self.bounds.height/5)
        backgroundView.center = CGPoint(x:self.bounds.size.width / 2.0, y: self.bounds.size.height / 2.0)
        backgroundView.clipsToBounds = true
        backgroundView.cornerRadius = 10
        spinner.center = center
    }
}

0 个答案:

没有答案