Animate intrinsicContentSize更改 [英] Animate intrinsicContentSize changes

查看:330
本文介绍了Animate intrinsicContentSize更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIView子类绘制一个圆的半径变化(与漂亮的弹跳动画)。视图决定了圆的大小。



我想让这个UIView子类更改其框架大小,以匹配动画变化为圆半径,我想要这些更改修改连接到视图的任何NSLayoutConstraints(以便限制到圆的边缘的视图将随着圆的大小移动)。



我理解实现 - (CGSize)intrinsicContentSize 并调用 invalidateIntrinsicContentSize 当半径变化会告诉约束更新,但我不知道如何动画对 intrinsicContentSize 的更改。



从[UIView animateWith ...]块内调用 invalidateIntrinsicContentSize

这是可能的,是否有解决方法/更好的方法?

解决方案

invalidateIntrinsicContentSize 适用于动画和 layoutIfNeeded 。你唯一需要考虑的是,改变内在内容大小会使superview的布局无效。所以这应该工作:

  [UIView animateWithDuration:0.2 animations:^ {
[self invalidateIntrinsicContentSize];
[self.superview setNeedsLayout];
[self.superview layoutIfNeeded];
}];


I have a UIView subclass that draws a circle whose radius changes (with nice bouncy animations). The view is deciding the size of the circle.

I want this UIView subclass to change its frame size to match the animated changes to the circle radius, and I want these changes to modify any NSLayoutConstraints connected to the view (so that views that are constrained to the edge of the circle will move as the circle resizes).

I understand that implementing -(CGSize)intrinsicContentSize and calling invalidateIntrinsicContentSize when the radius changes will tell constraints to update, but I cant figure out how to animate the changes to intrinsicContentSize.

Calling invalidateIntrinsicContentSize from within a [UIView animateWith... block just instantly updates the layout.

Is this even possible, and is there a workaround/better approach?

解决方案

invalidateIntrinsicContentSize works well with animations and layoutIfNeeded. The only thing you need to consider is, that changing the intrinsic content size invalidates the layout of the superview. So this should work:

[UIView animateWithDuration:0.2 animations:^{
    [self invalidateIntrinsicContentSize];
    [self.superview setNeedsLayout];
    [self.superview layoutIfNeeded];
}];

这篇关于Animate intrinsicContentSize更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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