动画在约束

时间:2015-05-04 07:34:50

标签: ios uitableview header

我有一个自定义UIView用作UITableView的节头。我希望能够通过更改标题视图的高度来扩展和折叠标题视图,并为此更改设置动画。

标题视图包含两个使用约束布局的标签。折叠视图时,放在底部的标签的高度约束将设置为0.

当我按下折叠/展开按钮时,我更改了由headerHeight返回的tableView:heightForHeaderInSection:变量。在这里,我还更改了高度约束的值。我同时在tableView.beginUpdates()tableView.endUpdates()内进行,但标题视图不会设置动画。在我开始滚动表格视图之前,它甚至不会使用新的高度。或者,我使用tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .Automatic)为标题视图的高度设置动画,但会弄乱其中的子视图(顶部标签在整个标题视图中垂直拉伸,即使它具有固定的高度)。

有没有人有一个解决方案可以使用约束对标题视图高度及其子视图进行适当的动画处理?

以下是HeaderView的代码:

class HeaderView: UIView {

    let titleLabel = UILabel()
    let subtitleLabel = UILabel()
    let expandButton = UIButton()

    var subtitleLabelHeightConstraint: NSLayoutConstraint!

    init() {
        super.init(frame: CGRectNull)

        titleLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
        subtitleLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
        expandButton.setTranslatesAutoresizingMaskIntoConstraints(false)

        expandButton.setTitle("Expand / Collapse", forState: .Normal)

        addSubview(titleLabel)
        addSubview(subtitleLabel)
        addSubview(expandButton)

        let views = ["titleLabel": titleLabel, "subtitleLabel": subtitleLabel, "expandButton": expandButton]
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[titleLabel]-[expandButton]|", options: nil, metrics: nil, views: views))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[subtitleLabel]|", options: nil, metrics: nil, views: views))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[titleLabel]-(>=0)-|", options: nil, metrics: nil, views: views))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-(>=0)-[subtitleLabel]|", options: nil, metrics: nil, views: views))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[expandButton]-(>=0)-|", options: nil, metrics: nil, views: views))

        titleLabel.addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 20))
        subtitleLabelHeightConstraint = NSLayoutConstraint(item: subtitleLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 20)
        subtitleLabel.addConstraint(subtitleLabelHeightConstraint)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

TableViewController的代码:

class TableViewController: UITableViewController {

    let headerView: HeaderView = {
        let view = HeaderView()
        view.titleLabel.text = "Title"
        view.subtitleLabel.text = "Subtitle"
        view.backgroundColor = UIColor.lightGrayColor()
        return view
    }()

    var headerHeight: CGFloat = 60

    override func viewDidLoad() {
        super.viewDidLoad()

        headerView.expandButton.addTarget(self, action: "toggleExpansion", forControlEvents: .TouchUpInside)
    }

    func toggleExpansion() {
        tableView.beginUpdates()

        if headerHeight == 60 {
            headerHeight = 40
            headerView.subtitleLabelHeightConstraint.constant = 0
        } else {
            headerHeight = 60
            headerView.subtitleLabelHeightConstraint.constant = 20
        }

        tableView.endUpdates()

        // Alternatively use tableView.reloadSections instead of begin and end updates:
        // tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .Automatic)
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
        cell.textLabel?.text = "Cell"
        return cell
    }

    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return headerHeight
    }

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return headerView
    }


}

我还使用代码在https://github.com/lammertw/DynamicSectionHeader创建了一个项目。

1 个答案:

答案 0 :(得分:0)

尝试替换此:

func toggleExpansion() {
    tableView.beginUpdates()

    if headerHeight == 60 {
        headerHeight = 40
        headerView.subtitleLabelHeightConstraint.constant = 0
    } else {
        headerHeight = 60
        headerView.subtitleLabelHeightConstraint.constant = 20
    }

    tableView.endUpdates()

    // Alternatively use tableView.reloadSections instead of begin and end updates:
    // tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .Automatic)
}

由此:

func toggleExpansion() {

    if headerHeight == 60 {
        headerHeight = 40
        headerView.subtitleLabelHeightConstraint.constant = 0
    } else {
        headerHeight = 60
        headerView.subtitleLabelHeightConstraint.constant = 20
    }

    tableView.beginUpdates()
    tableView.endUpdates()

    // Alternatively use tableView.reloadSections instead of begin and end updates:
    // tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .Automatic)
}
相关问题