如何动画UILabel?

时间:2011-11-08 14:46:47

标签: objective-c ios uilabel

我正在使用UILabel来显示一些动态文本。有时它超过标签的宽度。如果UILabel越界,我想滑动文本。

如何使用UILabel进行滑动动画?有什么帮助吗?

3 个答案:

答案 0 :(得分:3)

修改我的答案以解决短暂的问题 -

UILabel *label; //Previously initialized UILabel
float newX   = 0.0;
float newY   = 101.0f;
label.center = CGPointMake(newX, newY);

如果您想为动画制作动画,可以添加动画块:

UILabel *label; //Previously initialized UILabel
float newX = 90.0f;
float newY = 101.0f;

[UIView transitionWithView:label
                  duration:0.5f 
                   options:UIViewAnimationCurveEaseInOut
                animations:^(void) {
                     label.center = CGPointMake(newX, newY);
                } 
                completion:^(BOOL finished) {
                     // Do nothing
                }]; 

请注意,我们只根据您希望标签滑动来设置X坐标的动画。

答案 1 :(得分:0)

当我不得不做类似的事情时,我这样做了。 请注意我正在补充alpha,而不是位置,但我希望这可以帮助你:

[UIView animateWithDuration:0.3f animations:^{yourLabel.alpha = 0.f;}];

答案 2 :(得分:0)

-(void)repeat{


[NSTimer scheduledTimerWithTimeInterval:8.0
                                     target:self
                                   selector:@selector(LabelAnimation)
                                   userInfo:nil
                                    repeats:YES];


}


-(void)LabelAnimation
{

 NSArray *elements = [yourText componentsSeparatedByString:@","];
    NSMutableArray *outputElements = [[NSMutableArray alloc] init];
    for (int i=1;i<[elements count];i++)

            [outputElements addObject:[elements objectAtIndex:i]];


    NSString *outputString = [outputElements componentsJoinedByString:@","];

    [UIView animateWithDuration:3.0f delay:0.2f options:UIViewAnimationOptionTransitionNone animations:^{

        _header.labelHeaderName.text=outputString;
    } completion:^(BOOL finished)
     {
         if (outputString.length == 0) {
            _header.labelHeaderName.text=_strAddress(your saved address);
         }

     }];
    }