2012-08-29 59 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秒的嵌套動畫之上,因此仍然在屏幕上,等等,你最終會得到一堆重疊的標籤。