UILabel动画宽度

3

如何使用UIViewanimateWithDuration方法使UILabel的宽度增加和减少,仅限于宽度?

非常感谢您的帮助。(到目前为止,我已经尝试了很多次,但都没有成功)


分享一下你尝试过的。谢谢。 - Lorenzo B
5个回答

2

你可以在UIView动画块中设置UILabel的框架。像这样:

CGRect originalFrame = self.address.frame;

[UIView animateWithDuration:0.3
                 animations:^{
    self.myLabel.frame = CGRectMake(0, 0, 100, 100);
}
                 completion:^(BOOL finished) {
                     // use similar UIView animation to resize back to original
                 }];

显然,您输入的尺寸将取决于您的应用程序,并且可能基于内容进行计算。

如何打开它,然后再缩短它? - inforeqd
尝试过这个,但是它只会让标签消失,而不是在动画结束时关闭。[UIView animateWithDuration:1. delay:1. options:0 animations:^{ [self.messageLabel setFrame:CGRectMake(90, 0, 100, 90)]; } completion:^(BOOL finished) { [UIView animateWithDuration:2. animations:^{ [self.messageLabel setFrame:CGRectMake(90, 0, 0, 90)]; }]; }]; - inforeqd
这是我会采取的方法(我已更新我的答案以包含这些细节)。你最终的动画高度为零,所以它不会可见,我本以为它会动画化。 - Stew
尝试过这个(与上面的答案相同),标签会回弹到旧框架,而不是动画进入新框架。 - inforeqd

0
尝试使用以下动画来增加和减少帧宽度。
    // original label frame CGRectMake( 0,0, 100,60 );
    [UIView beginAnimations: @"moveLogo" context: nil]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationCurve: UIViewAnimationCurveLinear];
    [UIView setAnimationRepeatAutoreverses:YES];
    [UIView setAnimationRepeatCount:4];
    //increasing frame width
    label.frame =  CGRectMake( 0,0, 400,60 );   
    [UIView commitAnimations];

0

试试这个

 [UIView animateWithDuration:0.6 animations:^(void)
  {
     self.label.bounds = CGRectMake(100, 100, 200, 100); 

 }completion:^(BOOL finished) {

     [UIView animateWithDuration:0.6 animations:^(void)
     {
         self.label.bounds = CGRectMake(100, 100, 100, 100); 
     }];

 }];

在动画结束时,标签帧会直接跳到旧帧,并没有以动画形式进入旧帧。 - inforeqd

0

最终在iOS 5.0中使用块完成了它

        [UIView animateWithDuration:2. delay:0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionBeginFromCurrentState  animations:^{
            [self.messageLabel setFrame:CGRectMake(90, 0, labelSize.width, 90)];

        } completion:^(BOOL finished) {

        }];

0
我能够用这种方法来实现。 self 指的是我正在添加标签的视图。在 init 中,将标签添加为宽度为 1 像素。然后使用以下内容... UIViewAnimationOptionBeginFromCurrentState 可以让它像门一样打开。PHEW!
    [UIView animateWithDuration:1. delay:0 options:UIViewAnimationOptionBeginFromCurrentState  animations:^{
        [self setFrame:CGRectMake(self.frame.origin.x-labelSize.width-PADDING, self.frame.origin.y, self.frame.size.width+labelSize.width+PADDING, self.frame.size.height)];
        [self.messageLabel setFrame:CGRectMake(95, 10, labelSize.width, labelSize.height)];
        self.messageLabel.alpha = 1; 
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1. delay:2. options:0  animations:^{
            self.messageLabel.alpha = 0;  
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:1. delay:0. options:0  animations:^{
                [self setFrame:CGRectMake(self.frame.origin.x+labelSize.width+PADDING, self.frame.origin.y, self.frame.size.width-labelSize.width-PADDING, self.frame.size.height)];
            } completion:^(BOOL finished) {
            }];
        }];
    }];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接