1

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

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

[UIView animateWithDuration:1.0 animations:^{

    self.label1.frame = newFrame;
}];

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

4

2 回答 2

7

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

于 2012-07-12T19:10:28.797 回答
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;
}
于 2012-07-12T19:18:43.773 回答