带有清晰背景和彩色contentView的UitaiteViewCell



我想在我的 UITableViewCell周围创建一个"假"间距,并且我正在这样做,然后将contentview的框架插入10 [我实际上是在上面添加了一个自定义视图contentview并插入10]。看起来内容视图是唯一可见的视图。这看起来真的很好,我还为我的单元格设置和调整selectedBackgroundView的框架,以便选择只能选择"可见"区域。

现在这样做的问题是:

如果我选择一个单元格,它将用我的selectedBackgroundView指定的UIColor.darkGray闪烁。然后,在动画中的短时间内,我的单元背景完全是看不见的,然后才能回到它的状态。这样,动画看起来不会流利。

这适用于内容视图:

  1. 背景颜色
  2. darkgray(oplectbackgroundView)
  3. 透明颜色
  4. 背景颜色

有人知道我是否可以在选择成为事物的同时解决此行为?

我从动画中创建了一个gif:https://i.stack.imgur.com/iqme3.jpg

这是我的代码:

class BasicTableViewCell : UITableViewCell {
    public var basicContentView: UIView = UIView(frame: .zero)
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: .default, reuseIdentifier: reuseIdentifier)
        self.tintColor = UIColor.white
        self.contentView.backgroundColor = UIColor.clear
        self.basicContentView.backgroundColor = UIColor.barTintColor
        self.basicContentView.layer.masksToBounds = false
        self.basicContentView.layer.cornerRadius = 10.0
        self.basicContentView.translatesAutoresizingMaskIntoConstraints = false
        self.contentView.addSubview(self.basicContentView)
        self.basicContentView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 10.0).isActive = true
        self.basicContentView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -10.0).isActive = true
        self.basicContentView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 10.0).isActive = true
        let bottomConstraint = self.basicContentView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -10.0)
        bottomConstraint.priority = UILayoutPriority(999)
        bottomConstraint.isActive = true
        let selectedView: UIView = UIView(frame: .zero)
        selectedView.backgroundColor = UIColor.darkGray
        selectedView.layer.cornerRadius = 10.0
        selectedView.layer.masksToBounds = false
        self.selectedBackgroundView = selectedView
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        // only for selectedBackgroundView, contentView raised other issues
        let contentViewFrame = self.contentView.frame
        let insetContentViewFrame = contentViewFrame.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
        self.selectedBackgroundView?.frame = insetContentViewFrame
    }
}

我已经知道问题是什么,基于:苹果说明 selectedBackgroundView被添加到contentView中,然后逐渐消失,然后再从contentView删除,然后再次可见basicContentView,从而导致错误。

这是我的修复

override func setSelected(_ selected: Bool, animated: Bool) {
        if selected {
            self.basicSelectedBackgroundView.alpha = 1.0
            self.basicContentView.insertSubview(self.basicSelectedBackgroundView, at: 0)
        } else {
            guard self.basicSelectedBackgroundView.superview != nil else {
                return
            }
            if animated {
                UIView.animate(withDuration: 0.5, delay: 0.0, options: .allowUserInteraction, animations: {
                    self.basicSelectedBackgroundView.alpha = 0.0
                }) { (finished: Bool) in
                    if finished {
                        self.basicSelectedBackgroundView.removeFromSuperview()
                    }
                }
            } else {
                self.basicSelectedBackgroundView.alpha = 0.0
                self.basicSelectedBackgroundView.removeFromSuperview()
            }
        }
    }

我自己对自己进行了超越,并自己动画。

您可能想在单元格中覆盖setSelected。例如:

override func setSelected(_ selected: Bool, animated: Bool) {
    //If we don't get the contentView's backgroundColor here and then reset it after the call to super.setSelected, the contentView's backgroundColor will "disappear" when the cell is selected
    let contentViewColor = contentView.backgroundColor
    super.setSelected(selected, animated: animated)
    contentView.backgroundColor = contentViewColor
}

选择单元格时,将单元格的所有子视图的背景色属性设置为选择颜色,该颜色覆盖了您自己设置的任何背景色属性。因此,您可能需要在此处手动设置ContentView的背景彩色以获取所需的行为。

最新更新