如何在iPhone应用程序中以编程方式从左到右移动文本?

时间:2013-01-08 12:10:12

标签: ios text uiview uilabel uiviewanimation

我在iPhone应用程序中工作。我想在我的应用程序中显示来自webservice的一些文本/警报,例如移动文本(从左到右滚动动画)。我不知道怎么做?

来自webservice的警报消息因长度而异,警报消息计数也会有所不同。我想在我的UIViewController顶部滚动动画一个接一个地展示。在iPhone应用程序中可以做到吗?如果有可能意味着有人请帮我这样做?我什么都不知道。请帮帮我。期待你的帮助。提前谢谢。

编辑:我尝试过stackoverflow.com/questions/1369314/how-to-add-moving-text-in-iphone。它适用于来自底部的动画文本。我想左右显示。我试图改变代码,但我无法解决这个问题。请帮我。感谢

2 个答案:

答案 0 :(得分:1)

试试这个:

将标签设置在默认位置,X = 0,宽度= 320 Y和高度根据您的需要

将此代码写入您想要执行动画的位置

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:5];

lbl.frame = CGRectMake(320, lbl.frame.origin.y, lbl.frame.size.width, lbl.frame.size.height);

[UIView commitAnimations];

注意:的 此代码用于从左到右更改CGRectMake参数,方法是从右到左。

编辑 :(首次评论后)

将此动画代码放入NSTimer方法中,该方法在每5或6秒之后运行并以该方法执行 该方法看起来像这样:

  • 更改标签文字
  • 设置默认位置
  • 动画代码
祝你好运

答案 1 :(得分:1)

尝试以下代码

int i = 0;
float width = 0.0;
float origin_x = 0.0;

for (NSString *s in arrList)
{
    CGSize size;
    NSRange r;

    size = [s sizeWithFont:[UIFont fontWithName:@"*****" size:14.0] constrainedToSize:CGSizeMake(10000.0, 35.0) lineBreakMode:UILineBreakModeWordWrap];

    width = (size.width + 12.0 + 10.0);

    UIView *viewTopStory = [[[UIView alloc] initWithFrame:CGRectMake(origin_x, 0.0, width, 35.0)] autorelease];
    viewTopStory.backgroundColor = [UIColor clearColor];

    UIImageView *imgStar = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"star.png"] ] autorelease];
    imgStar.frame = CGRectMake(5, 8.0, 12, 12);
    [viewTopStory addSubview:imgStar];

    UILabel *lblTopstory = [[[UILabel alloc] initWithFrame:CGRectMake(22.0, 2.75, size.width, 30.0)] autorelease];
    lblTopstory.font = [UIFont fontWithName:@"*****" size:14.0];
    lblTopstory.backgroundColor = [UIColor clearColor];
    lblTopstory.text = s;
    [viewTopStory addSubview:lblTopstory];

    [_viewTopStory addSubview:viewTopStory];

    CGRect frame = _viewTopStory.frame;
    frame.size.width += width;
    _viewTopStory.frame = frame;

    origin_x += width;

    i++;
}

CAKeyframeAnimation *animation = [CAKeyframeAnimation
                                  animationWithKeyPath:@"transform"];
//NSMutableArray *marrframeValues = [[[NSMutableArray alloc]init] autorelease];
CATransform3D Translation0 = CATransform3DMakeTranslation(0.0, 0.0, 0.0);
CATransform3D Translation4 = CATransform3DMakeTranslation(_viewTopStory.frame.size.width, 0.0, 0.0);

NSArray *frameValues = [NSArray arrayWithObjects:[NSValue valueWithCATransform3D:Translation0], [NSValue valueWithCATransform3D:Translation4],
                        nil];

[animation setValues:frameValues];

NSArray *frameTimes = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:1.0], nil];

[animation setKeyTimes:frameTimes];

animation.fillMode = kCAFillModeBackwards;
//animation.autoreverses = YES;
animation.repeatCount = 10000;
animation.removedOnCompletion = NO;
animation.duration = 200.0;

[_viewTopStory.layer addAnimation:animation forKey:@"popup"];

我已经将_viewTopStory放置了初始固定帧,之后我根据文本修改了帧。

希望它会对你有所帮助。

相关问题