Picker视图将覆盖Swift 4.2中的模糊效果值

时间:2018-12-25 01:03:51

标签: ios swift uipickerview uiblureffect

我想使用pickerview增加或减少照片中模糊效果的水平。即使我减小该值,它也会为其添加连续的模糊效果。我该怎么办?

使用最新的Swift和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)       
}
}

1 个答案:

答案 0 :(得分:0)

您的Blur(blur: 0.3)是一个函数,而不是对象类,它将在不更改属性的情况下运行。并且您的func向您的父视图添加了一个子视图,但是在添加另一个之前,没有代码行可以删除它。因此它将被堆叠起来。

  1. 删除旧的模糊子视图,然后添加新的
  2. 首先创建默认的模糊视图,然后更改其属性。