动画UILabel不顺畅

时间:2011-06-14 00:57:30

标签: iphone objective-c

我正在尝试为UIlabel设置动画以首先变大,然后缩小回原始帧。扩大工作按预期,但不缩小。当我用下面的代码收缩标签时,尺寸会在原点移动之前先调整。这会导致两步动画不顺畅。

这是我的代码:

CGRect rect = label.frame;
[UIView animateWithDuration:.2
                      delay: 0.1
                    options: UIViewAnimationOptionBeginFromCurrentState
                 animations:^{
                     label.frame = CGRectMake(rect.origin.x + 4,
                                                                        rect.origin.y + 4,
                                                                        rect.size.width-8,
                                                                        rect.size.height-8);
                 }
                 completion:^(BOOL finished){
                 }];

3 个答案:

答案 0 :(得分:2)

您可以尝试将变换应用于动画块内的标签,而不是调整矩形。类似于增长/缩小动画的以下行:

label.transform = CGAffineTransformMakeScale(1.5, 1.5); //grow
label.transform = CGAffineTransformMakeScale(1, 1);     //shrink

答案 1 :(得分:2)

请尝试这个解决方案,我想这正是你要找的。我已经测试了它的工作情况,但如果你正在寻找这个,请试着让我知道。

-(IBAction)growanimate:(id)sender
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(shrinkanimate)];
    label.transform = CGAffineTransformMakeScale(2.0f, 2.0f); //This will double label from current size.
    [UIView commitAnimations];
}

-(void)shrinkanimate
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    label.transform = CGAffineTransformMakeScale(1.0f, 1.0f); //This will get it back to original size.
    [UIView commitAnimations];
}

答案 2 :(得分:1)

我猜问题是由于调整帧大小会触发调整大小通知这一事实。在缩小时可能会有更多的中断因为超级视图的新区域被揭示出来。

在这种情况下,转换方法要好得多。

我还猜测,使用transform方法,字符路径和布局(换行符等等)不会重新计算,缓存的CGPath只是在渲染时进行转换。


关于居中问题,我没有看到任何问题。

我想补充一点,如果你打算大量使用这个效果,你可以为这个效果创建一个静态类,或者包含几个预设的动画,里面有静态存储的变换。

然后你打电话[MyPopEffect popView:mylabel];

它可以避免创建和发布转换,并允许即时使用任何视图或其他项目。

无论如何,这是动画代码......干杯

[

UIView animateWithDuration:0.5f delay: 0.0f
    options:UIViewAnimationOptionCurveEaseOut+UIViewAnimationOptionBeginFromCurrentState
        animations:^{   
            label.transform=CGAffineTransformMakeScale(2.0f,2.0f);
        }
    completion:^(BOOL finished){
            [UIView animateWithDuration:0.5f delay: 0.0f
            options:UIViewAnimationOptionCurveEaseIn+UIViewAnimationOptionBeginFromCurrentState
            animations:^{
               label.transform=CGAffineTransformMakeScale(1.0f,1.0f);
            }
            completion:^(BOOL finished){}];
    }];

相关问题