【问题标题】:Animate the height of UILabel to 0将 UILabel 的高度设置为 0
【发布时间】:2012-07-12 14:42:10
【问题描述】:

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

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

[UIView animateWithDuration:1.0 animations:^{

    self.label1.frame = newFrame;
}];

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

【问题讨论】:

  • 当文字高度变化时,您希望/期望文字有什么效果?
  • 是您希望设置动画的高度还是位置?

标签: ios animation size height uilabel


【解决方案1】:

您可以将UILabel 包含在另一个UIView 中,将封闭UIView autoresizesSubviews' 属性设置为NOclipToBoundsYES,然后设置封闭@987654328 的高度@...

【讨论】:

    【解决方案2】:

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

        //[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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      • 2011-08-06
      相关资源
      最近更新 更多