【问题标题】:CALayer disappears on Custom UITableViewCell NIB after dequeue出队后,CALayer 在自定义 UITableViewCell NIB 上消失
【发布时间】:2018-09-17 16:42:32
【问题描述】:

我有一个 UITableView,其中每一行都是一个自定义的 XIB。一行有 4 个 UIButton,它们是选项,例如问题、信息等。当 UIButton 被点击时,我会在 CALayer 中显示动画。当table view滚动时,CALayer被移除,即动画消失。

当按钮被点击时,我创建了 CALayer 并且动画开始了。如何确保表格滚动或更新时 CALayer 不会消失?

class ReasonForSupportTableViewCell: UITableViewCell {

    let animationView = AnimationView()
    var hasButtonBeenTapped = false
    var previousButtonTapped: UIButton? = nil

    //IBOutlets
    @IBOutlet weak var questionButton: UIButton!
    @IBOutlet weak var informationButton: UIButton!
    @IBOutlet weak var crashButton: UIButton!
    @IBOutlet weak var bugButton: UIButton!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

        //set initial button
        questionButton.sendActions(for: .touchUpInside)

    }

    func createView(sender: UIButton) {

        //draw the frame
        animationView.frame = CGRect(x: 0, y: 0, width: 75.0, height: 75.0)

        //change the view background color
        animationView.backgroundColor = UIColor.clear
        animationView.isUserInteractionEnabled = false

        //add the view.
        sender.addSubview(animationView)


    }

    @IBAction func buttonTapped(_ sender: UIButton) {


        if previousButtonTapped == nil {

            //you haven't tapped a button yet

            createView(sender: sender)
            animationView.animateTo()

            previousButtonTapped = sender

        } else if previousButtonTapped == sender {

            animationView.removeAnimation()
            previousButtonTapped = nil

        } else {

            animationView.removeAnimation()

            createView(sender: sender)
            animationView.animateTo()

            previousButtonTapped = sender

        }

    }

}

【问题讨论】:

    标签: swift uitableview xib calayer nib


    【解决方案1】:

    子类 UIButton 以在初始化时添加 CALayer,而不是在点击按钮时添加。然后您可以使用prepareForReuse() 函数停止任何动画。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多