2012-08-29 132 views
1

我写了下面的代码,用于在标签上添加选取框效果,并在视图上逐一显示标签。UILabels Marquee +在动画标签上重叠

- (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]; 
    } 
}]; 
} 

一些如何,在阵列的字符串被显示以这样的方式,在执行动画,它们是重叠的。

任何想法,任何人?

让我知道,以防需要更多信息。

+0

您的标签是否将文本更新为位于nextIndex的数组中的字符串?什么marqueeMessage:strMessage呢? –

+0

我发布的代码本身就是函数'marqueeMessage:strMessage' ...让我再次编辑帖子... – viral

+0

@PratyushaTerli:是的,标签正在更新。但是它们彼此重叠。 – viral

回答

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]; 
} 

可能会帮助你。

0

我想你永远去除上海华标签......你需要保持引用您的标签,并做到:

[old_label removeFromSuperview]; 
+0

这是一些其他的事情,与我的问题没有关系。感谢您关注它... – viral

+0

请问您能为我们提供您的功能的完整代码吗?有一些缺失的变量,很难把所有的东西放在一起:) – PofMagicfingers

0

在我看来,你的第一个动画(其持续0.2秒)完成和完成被称为,它添加了另一个标签到你的视图,在持续20秒的嵌套动画之上,因此仍然在屏幕上,等等,你最终会得到一堆重叠的标签。