我编写了以下代码,用于在视图中逐个显示的标签上添加字幕效果。
- (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];
}
}];
}
数组中的字符串以这样一种方式显示,即它们在执行动画时重叠。
有人知道吗???
如果需要更多信息,请告诉我。
-(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];
}
可能对你有帮助。
我认为你永远不会从超视图中删除标签。。。你需要参考你的标签并这样做:
[old_label removeFromSuperview];
在我看来,你的第一个动画(持续0.2秒)完成了,并调用了完成,这在持续20秒的嵌套动画之上为你的视图添加了另一个标签,因此仍然在屏幕上,等等。你会得到一堆重叠的标签。