将模糊应用于透明弹出视图,快速

时间:2016-07-02 21:11:58

标签: swift uiview popup blur

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

我有这段代码:

已更新

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相当新鲜:)

1 个答案:

答案 0 :(得分:2)

尝试放置

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

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

- 更新 -

我建议您使用您创建的Popupview框架来创建visualeffectview。在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)