【问题标题】:Remove blur effect in tableview cell in Swift在 Swift 中删除 tableview 单元格中的模糊效果
【发布时间】:2016-01-15 19:09:11
【问题描述】:

我在我的 IF 子句中,在 cellForRowAtIndexPath 方法中这样做:

let blurEffect = UIBlurEffect(style: .Light)
        let visualEffect = UIVisualEffectView(effect: blurEffect)

        visualEffect.frame = cell.attachedImage.bounds

        cell.attachedImage.addSubview(visualEffect)

在我的 ELSE 子句中,我想从 cell.attachedImage 中删除效果。

我该怎么做?

【问题讨论】:

    标签: swift uiblureffect


    【解决方案1】:

    cellForRowAtIndexPath 不应向单元格添加子视图。这是有问题的,因为单元格会被重复使用,如果您不保留对已添加内容的引用,您最终会多次添加视图,这可能会导致滚动速度问题以及其他错误。

    正确的方法是创建一个已经设置好视图的单元子类。您的子类可以包含引用视图的属性,以便您可以根据需要更改、隐藏、移动或从其父视图中删除它们。但是这个逻辑应该在 cell 子类中。

    【讨论】:

    • 好电话。强调“隐藏”部分:单元格被重用,因此如果一个单元格实例删除了一行的模糊视图,那么该单元格将被重用于需要模糊的另一行,您将不得不重建模糊视图。隐藏/显示子视图的成本要低得多。
    【解决方案2】:

    你只需要从它的超级视图中移除视觉效果视图。诀窍是在细胞被回收时找到要删除的正确视图。最简单的方法是将其存储在自定义 UITableViewCell 实现中:

    class CustomTableViewCell: UITableViewCell {
        var visualEffectView: UIView?
        var attachedImage: UIImageView!
        // other views, including outlets
    
        func setupIsBlurred(isBlurred: Bool) {
            if isBlurred {
                if self.visualEffectView == nil {
                    let blurEffect = UIBlurEffect(style: .Light)
                    let visualEffectView = UIVisualEffectView(effect: blurEffect)
    
                    visualEffectView.frame = self.attachedImage.bounds
    
                    self.attachedImage.addSubview(visualEffectView)
    
                    self.visualEffectView = visualEffectView
                }
            } else {
                self.visualEffectView?.removeFromSuperview()
                self.visualEffectView = nil
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多