对透明弹出视图应用模糊,swift

Apply blur to transparent popup view, swift

我有一个向用户显示一些信息的弹出视图,目前该视图的框架是一个黑色的半透明框。我想对该框应用一些模糊以提高呈现给用户的内容的清晰度。

我有这个代码:

已更新

class PinDetails : UIView {

    var popupView:UIView!
    var txtName:UITextField!
    var img1:UIButton!
    var img2:UIButton!
    var img3:UIButton!
    var selectedIndex:Int!
    var dictLocation:[String:String]!

    var blur:UIBlurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)

    override init(frame: CGRect) {

        super.init(frame: frame)

        var effectView:UIVisualEffectView = UIVisualEffectView (effect: blur)
        effectView.frame = frame
        addSubview(effectView)

        popupView =  UIView(frame:TCRectMake(x: 25,y: 120,width: 270,height: 285))
        popupView.layer.cornerRadius = 20
        popupView.backgroundColor = UIColor(white:0, alpha: 0.5)
        self.addSubview(popupView)

我尝试实现的模糊代码似乎不起作用,xcode 指出: instance member 'blur' cannot be used on type 'PinDetails.

感谢任何帮助,我对 Swift 还很陌生 :)

尝试将您的

var effectView:UIVisualEffectView = UIVisualEffectView (effect: blur)
effectView.frame = frame
addSubview(effectView)

在 init() 内部。您正在使用尚未初始化的属性。

-- 更新--

我建议使用您创建的弹出视图的框架来创建视觉效果视图。在你的 init()

中试试这个
    // Create the popup view
    popupView =  UIView(frame:CGRectMake(25,120,270,285))
    popupView.layer.cornerRadius = 20
    popupView.backgroundColor = UIColor(white:0, alpha: 0.5)

    // Create the Effect View and apply to Popup view
    let effect = UIVisualEffectView(frame: CGRectMake(0,0,popupView.layer.frame.width, popupView.layer.frame.height))
    effect.effect = blur
    popupView.addSubview(effect)

    // Add the popupview to the main view
    self.addSubview(popupView)