James Patterson James Patterson - 5 months ago 84
Swift Question

Apply blur to transparent popup view, swift

I have a popup view appear to the user displaying some information, currently the frame for the view is a black, semi transparent box. I want to apply some blur to this box to improve the clarity of what is presented to the user.

I have this code:

UPDATED



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)


The blur code I tried to implement doesnt seem to work, xcode states:
instance member 'blur' cannot be used on type 'PinDetails
.

Any help appreciated, I am fairly new to Swift :)

Answer

Try placing your

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

Inside init(). You are using properties which are not yet initialized.

-- Update --

I'd recommmend using the frame for the Popupview you create to create the visualeffectview. Try this in your 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)