UILabels Marquee +重叠动画标签

时间:2012-08-29 11:19:51

标签: iphone marquee uianimation

我编写了以下代码,用于在视图上逐一显示的标签上添加选框效果。

- (void)marqueeMessage:(NSString *)messageString
{
UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(520, 0, 480, 21))];
label.text = messageString;
[self.view addSubview:label];

[UIView animateWithDuration:0.2

                 animations:^ {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:20];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
    label.frame = CGRectMake(-480, 0, 480, 21);
    [UIView commitAnimations];
}
                 completion:^(BOOL finished) {

    NSLog(@"Animation Done!");
    if (array.count > 0)
    {
        nextIndex++;
        NSString *strMessage = [array objectAtIndex:nextIndex];
        [self marqueeMessage:strMessage];
    }
}];
}

有些方面,数组中的字符串以这种方式显示,在执行动画时它们是重叠的。

任何想法,任何人???

如果需要更多信息,请告诉我。

3 个答案:

答案 0 :(得分:2)

-(void)viewDidLoad {

    [super viewDidLoad];
    array=[[NSArray alloc]initWithObjects:@"Scroll test",@"Scroll test1",@"Scroll test2",@"Scroll test3",@"Scroll test4",nil];
    [self marqueeMessage:[array objectAtIndex:0]];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)marqueeMessage:(NSString *)messageString {
    label = [[UILabel alloc] initWithFrame:(CGRectMake(0, 50, 90, 21))];
    //label.tag=nextIndex;
    label.text = messageString;
    [self.view addSubview:label];
    [UIView beginAnimations:@"LBL" context:nil];
    [UIView setAnimationDuration:3];
    [UIView setAnimationDidStopSelector:@selector(performThis:)];
    [UIView setAnimationDelegate:self];

    label.frame = CGRectMake(360,50,90,21);
    [UIView commitAnimations];
}

- (void) performThis:(id) sender {
    if (i<[array count]) {
        label.text = [array objectAtIndex:i];
        i++;
    }
    else
    {
        i=0;
        label.text = [array objectAtIndex:i];
    }
    label.frame = CGRectMake(-90,50,90,21);
    [UIView beginAnimations:@"LBL" context:nil];
    [UIView setAnimationDuration:3];
    label.frame = CGRectMake(360,50,90,21);
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(performThis:)];
    [UIView commitAnimations];
}

可以帮到你。

答案 1 :(得分:0)

我认为你永远不会从superview中删除标签......你需要继续引用你的标签并这样做:

[old_label removeFromSuperview];

答案 2 :(得分:0)

在我看来,你的第一个动画(持续0.2秒)结束并调用完成,这会在你的视图中添加另一个标签,在嵌套动画的基础上持续20秒,因此仍然在屏幕上等等。你最终会得到一堆重叠的标签。