简体   繁体   English

在UITableView中单击单元格按钮上的单元格图像模糊

[英]Blur cell image on cell button click in UITableView

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
}

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 您必须在特定的单元格上添加模糊视图,在其中单击.....,然后可以通过按钮标签将单元格添加到func

 @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. 但是您必须保存模糊单元格的indexPath或行,并在每次在cellForRowAtIndex方法中重新加载表时绘制它。 Because when table view reload or reuse same cell , it will show blur. 因为当表视图重新加载或重用同一单元格时,它将显示模糊。

Change following func (Assuming it is in MealCell class)... 更改以下函数(假设它在MealCell类中)...

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 ie MealCell class. 您可以在表MealCell单元格即MealCell类中创建按钮的IBAction。

@IBAction func tickButton(_ sender : UIButton){
    blurEffect() 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM