1

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

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

不知何故,数组中的字符串以这样的方式显示,即它们在执行动画时重叠。

任何想法,任何人???

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

4

3 回答 3

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

可以帮助你。

于 2012-08-30T04:03:50.507 回答
0

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

于 2012-08-29T12:47:38.707 回答
0

我认为您永远不会从 superview 中删除标签...您需要保留对标签的引用并这样做:

[old_label removeFromSuperview];
于 2012-08-29T12:22:59.550 回答