动画UILabel尺寸减少
当增加标签的高度时,一切都很好,很平滑。减少时,标签会立即改变尺寸,然后用动画重新定位。动画UILabel尺寸减少
@interface
@property (nonatomic, retain) IBOutlet UILabel *explanationLabel;
@implementation
CGRect frmExpl = explanationLabel.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.75];
frmExpl.size.height -= height;
explanationLabel.frame = frmExpl;
[UIView commitAnimations];
我试着用UIView替换UILabel,当然UIView没有这样的问题。
是否有任何特殊的方式来动画UILabel大小减少动画?
这是一个展示所述问题的最小项目。 Download
我想你想改变的是边界而不是框架。从该文档:
“的边界矩形来确定视图的坐标系中的原点和规模其框架矩形中和在的点处测量设置这个属性相应地改变框架属性的值。” - UIView类; bounds属性
试着这么做:
- (void)animate:(id)sender
{
...
CGRect newBounds = testLabel.bounds;
newBounds.size.height += 50;
testLabel.bounds = newBounds;
...
}
增加界限的确改变了框架,但框架屏幕上保持不变。减少边界的作品。但这不是解决方案,因为我需要增加和减少。我怀疑这是一个框架中的错误。 – Pablo 2010-07-22 00:06:30
确实,这可能是一个错误。你能否改变你的视图层次结构,使其具有蓝色背景的UIView,然后是固定大小的UILabel,它的struts和spring被设置为浮在UIView的中间。鉴于UIView动画正确,可能是一个解决方案。 – 2010-07-22 20:59:49
的问题是,作为的UILabel只要其大小变化重绘自己。 (它不能重绘动画的每一帧,因为文本渲染发生在CPU上,而不是UIView动画运行的GPU)。通过将标签的contentMode属性更改为例如UIViewContentModeCenter,可以避免重绘。
有事变了吗?经过测试,这似乎不工作.. – 2014-10-18 11:00:48
非常感谢,为我工作 – 2014-11-15 16:42:06
浪费了2天找出这个设置,谢谢! – dreampowder 2017-11-17 14:46:56
使用CGAffineTransform
来做到这一点。
[UIView animateWithDuration:1.0 animations:^{
// Scale down 50%
label.transform = CGAffineTransformScale(label.transform, 0.5, 0.5);
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.0 animations:^{
// Scale up 50%
label.transform = CGAffineTransformScale(label.transform, 2, 2);
}];
}];
你从哪里得到frmExpl? – deanWombourne 2010-07-21 11:46:23
从nib文件中设计的标签框架。更新。 – Pablo 2010-07-21 11:50:32
http://*.com/questions/17360402/why-are-animations-on-bounds-of-an-uilabel-only-working-when-increasing-the-size – 2014-03-06 06:15:51