iosswiftuipickerviewuiblureffect

Picker view overwrites the value of blur effect in swift 4.2


I want to increase or decrease the level of blur effect in the photo using the pickerview. Even if I decrease the value, it adds a continuous blur effect to it. What should I do?

Working on latest Swift and Xcode

class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {

@IBOutlet var bg: UIImageView!
@IBOutlet var pickView: UIPickerView!

func numberOfComponents(in pickerView: UIPickerView) -> Int {

    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    return arr.count
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    switch arr[row] {
    case "1":
        Blur(blur: 0.3)
    case "2":
        Blur(blur: 0.6)
    case "3":
        Blur(blur: 0.0)
    default:
        print("not change")
    }
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return arr[row]
}
let arr = ["1","2","3"]
override func viewDidLoad() {
    super.viewDidLoad()
    pickView.dataSource = self
    pickView.delegate = self
    // Do any additional setup after loading the view, typically from a nib.
}
func Blur(blur:CGFloat)
{
    let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.light)
    let blurView = UIVisualEffectView(effect: blurEffect)
    blurView.frame = bg.bounds
    blurView.alpha = blur
    self.view.addSubview(blurView)       
}
}

Solution

  • Your Blur(blur: 0.3) is a function not an object class, it will run not change properties. And your func added a Subview to your parent View but none code line to remove it before add another. So it will be stack up.

    1. Remove old blur subView then add new.
    2. Create a default blur view first then change it properties.