通过更改其优先级值来动画布局约束 [英] Animate a layout constraint by changing its priority value

查看:55
本文介绍了通过更改其优先级值来动画布局约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量文本的标签.有一个用于折叠和展开标签高度的开关(这里它被命名为 "lire la suite"),因此它会截断文本的结尾.

I have a label containing quite a lot of text. There's a toggle for collapsing and expanding the height of the label (here it's named "lire la suite") so it truncates the end of the text.

我精心设置了垂直内容拥抱优先级和抗压性,因此内在尺寸优先于抗压性.

I have meticulously set the vertical content hugging priority and compression resistance so the intrinsic size has higher priority over the compression resistance.

高度约束(标签右侧的可选约束)设置为常量71,只是4行的高度.它永远不会改变.

The height constraint (the optional constraint directly at the right of the label) is set with a constant of 71, just the height for 4 lines. It never changes.

然后这个相同的约束在 747749 之间有一个优先级切换,所以会发生以下情况:

Then this same constraint has a priority switching between 747 and 749 so the following happens:

  • 高度约束优先级 = 749:

抗压性<约束优先级<拥抱优先

压缩阻力在约束优先级下崩溃,如果其内在尺寸(拥抱优先级)较小,则其高度为71或更小.

Compression resistance collapses under the constraint priority, its height is 71 or less if its intrinsic size (hugging priority) is smaller.

  • 高度约束优先级 = 747:

    约束优先级<抗压性<拥抱优先

    更大的压缩阻力迫使高度遵循其固有尺寸.

    The greater compression resistance forces the height to follow its intrinsic size.

  • 这完美地工作.我的问题是我无法弄清楚如何为这个约束设置动画,因为每个解决方案都会为 constant 属性设置动画,而不是 priority.

    This works perfectly. My issue is that I can't figure out how to animate this constraint, since every solution animates the constant property and not the priority.

    我想知道是否有解决方案或变通办法.

    I would like to know if there's a solution or a workaround.

    推荐答案

    通过试验,您似乎无法使用优先级对约束进行动画处理,并且您被困在激活/停用约束或更改其常量上.

    By experimenting with it, it seems that you cannot animate constraints using priorities, and you are stuck either with activating/deactivating constraints, or changing their constants.

    >

    几天前我有过类似的任务.一个简单但有点天真的方法是删除约束并仅使用内在内容大小 - 您可以设置 label.numberOfLines = 4 当它应该折叠时(因此大小不会扩展到 4行)和 label.numberOfLines = 0 展开时.这非常简单和干净,但是,我不确定动画效果如何.

    I've had a similar task a couple of days ago. An easy but a bit naive approach is to drop the constraint and use only intrinsic content size - you can set the label.numberOfLines = 4 when it should be collapsed (thus the size won't expand over 4 lines), and label.numberOfLines = 0 when expanded. This is very easy and clean, however, I am not sure how that goes with animation.

    第二种方法是仅使用约束并为常量设置动画.您已经有 4 行的高度,您只需要展开标签的高度.您可以在 UILabel 上使用以下扩展来计算高度:

    A second approach is to use only the constraint and animate the constant. You have a height for 4 lines already, all you need is the height of the expanded label. You can use following extension on UILabel to calculate the height:

    extension UILabel {
        func heightNeeded() -> CGFloat {
            self.layoutIfNeeded()
            let myText = self.text! as NSString
            let boundingRectangle = CGSize(width: self.bounds.width, height: CGFloat.greatestFiniteMagnitude)
            let labelSize = myText.boundingRect(with: boundingRectangle,
                                                options: .usesLineFragmentOrigin,
                                                attributes: [NSAttributedStringKey.font: self.font],
                                                context: nil)
            return labelSize.height
        }
    }
    

    那么你需要做的就是:

    labelHeightConstraint.constant = label.heightNeeded()
    

    不要忘记如何使用自动布局为常量设置动画,例如参见我的以下答案到另一个 SO 问题.

    Don't forget on how to animate that constant using autolayout, see for example my following answer to another SO question.

    这篇关于通过更改其优先级值来动画布局约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆