将UILabel的高度设置为0

时间:2012-07-12 18:58:58

标签: ios animation size height uilabel

我有一个UILabel,我想做的是将它的高度平滑地改为0.

CGRect newFrame = self.label1.frame;
newFrame.size.height = 0;

[UIView animateWithDuration:1.0 animations:^{

    self.label1.frame = newFrame;
}];

问题是动画干扰了一些奇怪的事情。我想标签正试图调整大小并重新定位它的文本,这就是为什么它不起作用。我尝试过标签上所有可能的属性组合,但没有成功。

2 个答案:

答案 0 :(得分:7)

您可以将UILabel封闭到另一个UIView中,设置封闭的UIView autoresizesSubviews'属性NOclipToBoundsYES,然后为封闭的UIView的高度设置动画...

答案 1 :(得分:3)

尝试使用转换。我为你挖出了这段代码:

    //[self setAnchorPoint:CGPointMake(0.5, 1.0) forView:self.label];
    [UIView animateWithDuration:0.3 
                      delay:0
                    options:UIViewAnimationCurveEaseInOut 
                 animations:^{ 
                     //Scale the height to close to zero
                     //0.00001, because 0.0 behaves strange
                     self.label.transform = CGAffineTransformMakeScale(1, 0.00001);
                 } 
                 completion:^(BOOL finished) {
                     self.label.hidden = YES;
                 }];

这会将它缩小到中间。如果要将其缩小到顶部或底部,则需要设置锚点。我也有一些代码:

- (void) setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view {
CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y);
CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y);

newPoint = CGPointApplyAffineTransform(newPoint, view.transform);
oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform);

CGPoint position = view.layer.position;

position.x -= oldPoint.x;
position.x += newPoint.x;

position.y -= oldPoint.y;
position.y += newPoint.y;

view.layer.position = position;
view.layer.anchorPoint = anchorPoint;
}