0

In table view cell i have an image and a button now i want to blur the image on the click of button

I have the following code :-

func blurEffect(){
    let blur = UIBlurEffect(style: UIBlurEffectStyle.light)
    let blurView = UIVisualEffectView(effect: blur)
    var imageView = UIImageView()
    blurView.frame = imageView.bounds
    view.addSubview(blurView)
}

// I am Calling blur effect on at tick button action as below


@objc func tickButton(btn : UIButton){
    blurEffect() 
}


 //In tableView

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = MealTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MealCell

    cell.imgMeal.image = UIImage(named: mealImage[indexPath.row])

    cell.btnTick.addTarget(self, action: #selector(MealPlanViewController.tickButton(btn:)), for: .touchUpInside)
    return cell
}
SPatel
  • 4,768
  • 4
  • 32
  • 51
piyush ranjan
  • 391
  • 4
  • 15

2 Answers2

2

Try this code -

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = MealTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MealCell

    cell.imgMeal.image = UIImage(named: mealImage[indexPath.row])
    cell.btnTick.tag = indexPath.row
    cell.btnTick.addTarget(self, action: #selector(MealPlanViewController.tickButton(btn:)), for: .touchUpInside)
    return cell
}

you have to add blur view on particular cell , in which you click ..... and you can get cell in func by button tag like

 @objc func tickButton(btn : UIButton){
    let index = IndexPath.init(row: btn.tag, section: 0)
    let cell = tbl.cellForRow(at: index) as! newTbl
    let blur = UIBlurEffect(style: UIBlurEffectStyle.light)
    let blurView = UIVisualEffectView(effect: blur)
    blurView.frame = cell.img.bounds
    cell.img.addSubview(blurView)
}

But you have to save blur cell's indexPath or row and draw it every time when table reload in cellForRowAtIndex method. Because when table view reload or reuse same cell , it will show blur.

Deepti Raghav
  • 852
  • 1
  • 9
  • 26
-1

Change following func (Assuming it is in MealCell class)...

func blurEffect() {

  let blur = UIBlurEffect(style: UIBlurEffectStyle.light)
  let blurView = UIVisualEffectView(effect: blur)

  blurView.frame = self.imgMeal.bounds
  self.addSubview(blurView)
}

You can create IBAction of button with in tableview cell i.e. MealCell class.

@IBAction func tickButton(_ sender : UIButton){
    blurEffect() 
}
Mahendra
  • 8,448
  • 3
  • 33
  • 56